如何从 ping 结果中获取数字?

How to fetch numbers from ping result?

我在sunOS上做了ping实验ping -s www.google.com 56 5,结果是:

PING www.google.com: 56 data bytes
64 bytes from iad23s26-in-f18.1e100.net (173.194.121.50): icmp_seq=0. time=8.72 ms
64 bytes from iad23s26-in-f18.1e100.net (173.194.121.50): icmp_seq=1. time=8.69 ms
64 bytes from iad23s26-in-f18.1e100.net (173.194.121.50): icmp_seq=2. time=8.61 ms
64 bytes from iad23s26-in-f18.1e100.net (173.194.121.50): icmp_seq=3. time=8.54 ms
64 bytes from iad23s26-in-f18.1e100.net (173.194.121.50): icmp_seq=4. time=8.62 ms

----www.google.com PING Statistics----
5 packets transmitted, 5 packets received, 0% packet loss
round-trip (ms)  min/avg/max/stddev = 8.54/8.64/8.72/0.073

我需要的是收到的数据包数量5、丢包数0、min8.45、avg8.64和max8.72

我试图使用 > 将结果存储在文件中。我要的是5, 0, 8.45, 8.64, 8.72.

我可以使用 grep 来执行此操作吗?你有更好的方法吗?

我会带你走完大部分路。

ping -s www.google.com 56 5 | awk '/transmitted/ {print ,,}; /round-trip/ {print }' | sed -e 's/[\/\% ]/,/g'

这将使您受益:

5,5,0,
8.54,8.64,8.72,0.073

从这里开始,您只需将它分配到 bash 中的一个变量中,然后按照您认为合适的方式对其进行操作:

RESULT=`ping -s www.google.com 56 5 | awk '/transmitted/ {print ,,}; /round-trip/ {print }' | sed -e 's/[\/\% ]/,/g'`