我有一个格式为时间戳的文件:

<代码> HH:MM:SS.MS

e.g。

  

00:04:02.08

     

00:04:01.08

每个时间戳都在不同的行上,通常只是文件中的两行

我需要编写一个awk脚本来计算这些时间的平均值。我在 awk 脚本中非常天真,所以如果有人可以给我一个代码片段,那将会有很多帮助。

即使是shell脚本(bash)解决方案也会有所帮助。

有帮助吗?

解决方案


/..:..:..\./ {
  ++count
  split(
$ 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
$ 
,a,":"); seconds = (a[1] * 60.0 + a[2]) * 60.0 + a[3] print <*>, 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) } }

运行它......


<*>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top