如何尽快找到差距?

时间:2016-06-30 08:02:33

标签: bash

[root@test143 ~]# ping 8.8.8.8 | while read xx; do echo "$(date '+%Y-%m-%d %H:%M:%S'): $xx"; done
2016-06-30 15:51:41: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2016-06-30 15:51:41: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=47 time=78.2 ms
2016-06-30 15:51:43: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=47 time=78.2 ms
2016-06-30 15:51:44: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=47 time=78.3 ms
...
2016-06-30 15:57:58: 64 bytes from 8.8.8.8: icmp_seq=300 ttl=47 time=78.4 ms

我想监控网络中断(centos 6.5)。 如上所述,我们失去了 icmp_seq = 2 ,如何找到差距。

1 个答案:

答案 0 :(得分:0)

Search Stack again...

假设您有一个名为gap

的输入文件
more gaps
icmp_seq=1
icmp_seq=2
icmp_seq=3
icmp_seq=5
icmp_seq=8
icmp_seq=10

您可以使用以下AWK

awk -F "=" '$2!=p+1{print p+1"-"$2-1}{p=$2}' gaps
4-4
6-7
9-9

解释

-F "=" use = as the field separator
$2 is the second column from current input line
p is the previous value of the last line
so ($2!=p+1) is a condition : if $2 is different than previous value +1, then :
this part is executed : {print p+1 "-" $2-1} : print previous value +1, the - character and second columns + 1
{p=$2} is executed for each lines : p is assigned to the current 2nd column