views:

139

answers:

1

I have a file with timestamps in the format:

HH:MM:SS.MS

e.g.

00:04:02.08

00:04:01.08

Each timestamp is on a different line, usually just two lines in a file

I need to write an awk script to calculate average of these times. I am quite naive in awk scripting, so if someone can please give me a code-snippet, it will a lot of help.

Even a shell script (bash) solution will help.

+6  A: 


/..:..:..\./ {
  ++count
  split($0,a,":");
  seconds = (a[1] * 60.0 + a[2]) * 60.0 + a[3]
  print $0, seconds
  alltime += seconds
}
END {
  if (count > 0) {
    avgtime = alltime / count
    print count, alltime, avgtime
    mins = int(avgtime / 60.0) % 60
    hours = int(avgtime / 60.0 / 60.0)
    secs = avgtime % 60.0
    printf("%02d:%02d:%05.2f\n", hours, mins, secs)
  }
}


And running it...


$ cat test.data
other stuff

    00:04:02.08
more stuff

    00:04:01.08

more stuff
$ awk -f q.awk < test.data
    00:04:02.08 242.08
    00:04:01.08 241.08
2 483.16 241.58
00:04:01.58
$


DigitalRoss
If you are selling this on rent-a-coder please don't tell me about it. :-)
DigitalRoss
+1 But the regex could be improved maybe something like /[0-9]{1,}:[0-5][0-9]:[0-5][0-9]\.{1}[0-9]{0,3}/ as an entry like "#Time should be specified as HH:MM:SS.UUU" would through the average off
Steve Weet
Thanks a lot, be assured I ain't selling this at RentACoder. :) Need it as a part of a batch script i need to write at my work.
Mohit Nanda