1

How can I sum timing values with awk?

I got a list like this:

5:45
4:28
4:46
5:13
4:58
3:18
4:59
4:55
3:57
2:1
1:30
2:0
5:18
3:45
6:15

and would like an output like this:

h:mm:ss
4

Try

$ awk -F: '{a+=$1*60+$2}END{printf"%d:%02d:%02d",a/3600,a%3600/60,a%3600%60}' file
1:03:08
$
  • -F: - set : as field delimiter
  • {a+=$1*60+$2} - total up the number of seconds, being field 1 * 60 + field 2.
  • END{printf"%d:%02d:%02d" - at end of file, print the results in a formatted fashion, i.e. leading zeroes on minutes and seconds
  • a/3600, - hours is seconds divided by 60 divided by 60
  • a%3600/60, - minutes is remainder of seconds divided by 60 divided by 60, then divided by 60
  • a%3600%60} - for seconds, take remainder of seconds divided by 60 divided by 60, and then take the remainder of that divided by 60

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.