一行perl查找top n值

在网络测试中,ping是很常用的,ping一段时间,打印如下的log

64 bytes from 10.0.101.209: icmp_seq=1 ttl=64 time=1.10 ms
64 bytes from 10.0.101.209: icmp_seq=331 ttl=64 time=1.98 ms
64 bytes from 10.0.101.209: icmp_seq=800 ttl=64 time=1.30 ms
64 bytes from 10.0.101.209: icmp_seq=290 ttl=64 time=1.30 ms
64 bytes from 10.0.101.209: icmp_seq=975 ttl=64 time=1.23 ms
64 bytes from 10.0.101.209: icmp_seq=935 ttl=64 time=1.17 ms
64 bytes from 10.0.101.209: icmp_seq=8 ttl=64 time=1.42 ms
64 bytes from 10.0.101.209: icmp_seq=825 ttl=64 time=1.58 ms
64 bytes from 10.0.101.209: icmp_seq=3 ttl=64 time=1.32 ms
64 bytes from 10.0.101.209: icmp_seq=1 ttl=64 time=1.00 ms
64 bytes from 10.0.101.209: icmp_seq=999 ttl=64 time=1.23 ms
64 bytes from 10.0.101.209: icmp_seq=986 ttl=64 time=1.21 ms
64 bytes from 10.0.101.209: icmp_seq=1 ttl=64 time=1.08 ms
64 bytes from 10.0.101.209: icmp_seq=5 ttl=64 time=1.28 ms

如何查查最大的几个网络延时呢?写脚本?不用,一行perl搞定了
如果文件为ping.log,想查找top 5 延时,下面一行可以搞定

[root@test5 ~]#  perl -ne 'BEGIN { $arr[7]=0 } if(/time=([\d\.]+)/ and $1>$arr[0]){$arr[0]=$1;@arr=sort @arr } END { shift(@arr);  print "@arr\n" }' ping.log 
1.30 1.30 1.32 1.42 1.58

猜你喜欢

转载自blog.csdn.net/juewuer/article/details/80016274