使用awk计算持续时间

时间:2014-05-19 17:44:21

标签: awk

如何使用awk计算以下文件的持续时间:

02 0F 00 80 C9 CD AB 00 00 48 CF 00 00 00 00 00 00 6B E7 01 07 1A 16 1B 36 00  with Timestamp2014-03-12 18:46:59.986000
02 0F 00 80 CA CD AB 00 00 48 CF 00 00 00 00 00 00 6B E7 01 07 1A 14 DB 39 00  with Timestamp2014-03-12 18:47:02.446279
02 13 23 C8 B0 CD AB 00 00 FF FF E2 75 AC 21 4E 83 00 00 01 83 6C E7 01 11 1A 36 DD 39 00  with Timestamp2014-03-12 18:47:02.455278
02 03 12 00 B0 6B E7 01 03 1A FF FF FF FF  with Timestamp2014-03-12 18:47:02.457279
02 17 00 80 CB CD AB 00 00 48 CF 00 10 E2 75 AC 21 4E 83 00 00 00 00 00 00 6B E6 01 07 1A 10 9B 3D 00  with Timestamp2014-03-12 18:47:06.196279
02 10 63 C8 B1 CD AB 00 00 E2 75 AC 21 4E 83 00 00 04 6A E6 01 0F 1A 14 9D 3D 00  with Timestamp2014-03-12 18:47:06.205278
02 03 12 00 B1 6C E6 01 03 1A FF FF FF FF  with Timestamp2014-03-12 18:47:06.206279 

换句话说,最后一行时间戳值减去第一行时间戳值。

2 个答案:

答案 0 :(得分:0)

这是你想要的(使用GNU awk作为mktime())吗?

$ cat tst.awk
sub(/.*Timestamp/,"") {
    split($0,t,/[-:. ]/)
    time[++nr] = mktime(t[1]" "t[2]" "t[3]" "t[4]" "t[5]" "t[6]) "." t[7]
}
END { print time[nr] - time[1] }

$ awk -f tst.awk file
6.22028

答案 1 :(得分:0)

非常感谢@Ed Morton;它不适用于这个例子:

Number of saved packets= 373
Protocol: IEEE 802.15.4
Packet #0 :02 0F 00 80 C9 CD AB 00 00 48 CF 00 00 00 00 00 00 6B E7 01 07 1A 16 1B 36 00  with Timestamp2014-03-12 18:46:59.986000
###################################################################################

Packet #1 :02 0F 00 80 CA CD AB 00 00 48 CF 00 00 00 00 00 00 6B E7 01 07 1A 14 DB 39 00  with Timestamp2014-03-12 18:47:02.446279
###################################################################################

Packet #2 :02 13 23 C8 B0 CD AB 00 00 FF FF E2 75 AC 21 4E 83 00 00 01 83 6C E7 01 11 1A 36 DD 39 00  with Timestamp2014-03-12 18:47:02.455278
###################################################################################

Packet #3 :02 03 12 00 B0 6B E7 01 03 1A FF FF FF FF  with Timestamp2014-03-12 18:47:02.457279
###################################################################################

Packet #4 :02 17 00 80 CB CD AB 00 00 48 CF 00 10 E2 75 AC 21 4E 83 00 00 00 00 00 00 6B E6 01 07 1A 10 9B 3D 00  with Timestamp2014-03-12 18:47:06.196279
###################################################################################

Packet #5 :02 10 63 C8 B1 CD AB 00 00 E2 75 AC 21 4E 83 00 00 04 6A E6 01 0F 1A 14 9D 3D 00  with Timestamp2014-03-12 18:47:06.205278
###################################################################################

Packet #6 :02 03 12 00 B1 6C E6 01 03 1A FF FF FF FF  with Timestamp2014-03-12 18:47:06.206279