如何从bash脚本中的文件中提取ping参数

时间:2015-01-04 10:56:28

标签: bash ping

我有像这样的ping文件

PING 172.17.9.1 (172.17.9.1) 1000(1028) bytes of data.
1008 bytes from 172.17.9.1: icmp_seq=1 ttl=64 time=0.943 ms
1008 bytes from 172.17.9.1: icmp_seq=2 ttl=64 time=0.855 ms
1008 bytes from 172.17.9.1: icmp_seq=3 ttl=64 time=0.860 ms
.
.
--- 172.17.9.1 ping statistics ---
100 packets transmitted, 100 received, 0% packet loss, time 9958ms
rtt min/avg/max/mdev = 0.836/1.710/37.591/4.498 ms

我想用bash脚本提取数据包丢失,时间,平均值rtt。我该怎么办?! 感谢

2 个答案:

答案 0 :(得分:1)

您可以awk作为

$ awk -F"[,/]" '/packet loss/{print $3} /rtt/{print " rtt",$2,$5}' input
 0% packet loss
 rtt avg 1.710

答案 1 :(得分:1)

尝试类似:

awk -F',|/' '/time/{x=$3$4}/rtt/{print x " " $5}' ping.txt | sed 's/[^0-9 .]*//g'
Output:
0 958 1.710