需要帮助找到“WAM1”和“WAM2”最近连续启动之间的时差(仅秒)/ var / log / messages
WAM1在2013-07-09T02:20:10.735639Z
和2013-07-09T02:20:12.635639Z
上发布,所以现在最近的WAM2是2013-07-09T02:20:12.635639Z
,因此(15.84675 - 12.635639)= 3.211111
之间的差异是以秒为单位的结果
2013-07-09T02:20:10.735639Z user.info WbAppMg WAM1
2013-07-09T02:20:11.817372Z user.info sam SAM
2013-07-09T02:20:12.635639Z user.info WbAppMg WAM1
2013-07-09T02:20:13.817372Z user.info sam SAM
2013-07-09T02:20:14.818442Z user.info sam SAM
2013-07-09T02:20:15.846751Z user.info WbAppMg WAM2
2013-07-09T02:20:16.846636Z user.info sam SAM
2013-07-09T02:20:16.848751Z user.info WbAppMg WAM2
2013-07-09T02:20:17.851727Z user.info sam SAM
2013-07-09T02:20:18.935639Z user.info WbAppMg WAM1
2013-07-09T02:20:19.535639Z user.info WbAppMg WAM1
2013-07-09T02:20:20.817372Z user.info sam SAM
2013-07-09T02:20:21.818442Z user.info sam SAM
2013-07-09T02:20:22.746751Z user.info WbAppMg WAM2
2013-07-09T02:20:23.846636Z user.info sam SAM
2013-07-09T02:20:24.851727Z user.info sam SAM
以下程序为上述日志消息提供了错误的结果
$ awk '
/WAM1/{ split($1,start,/[:Z]/); wam1[++i]=start[3] }
/WAM2/{ split($1,stop,/[:Z]/); wam2[++j]=stop[3] }
END { len=i>j?i:j; for(;x<len;)
print "Delta "++x" = "wam2[x]-wam1[x] }' /var/log/messages
答案 0 :(得分:1)
请尝试以下代码:
awk '/WAM1/{ split($1,start,/[:Z]/); wam1=start[3] }
/WAM2/{ split($1,stop,/[:Z]/); wam2=stop[3]; print "Delta "++x" = "wam2-wam1}' 1
此代码产生:
Delta 1 = 3.21111
Delta 2 = 4.21311
Delta 3 = 3.21111
另一个代码:
awk '/WAM1/{ split($1,start,/[:Z]/); wam1=start[3]; i=1; }
/WAM2/{ split($1,stop,/[:Z]/); wam2=stop[3]; if (i==1) print "Delta "++x" = "wam2-wam1; i=0;}' 1
的产率:
Delta 1 = 3.21111
Delta 2 = 3.21111