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

时间:2015-04-30 15:08:07

标签: unix grep solaris ping

我在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,分钟8.45,平均8.64和最大8.72

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

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

1 个答案:

答案 0 :(得分:1)

我会带你走大部分路。

ping -s www.google.com 56 5 | awk '/transmitted/ {print $1,$4,$7}; /round-trip/ {print $5}' | 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 $1,$4,$7}; /round-trip/ {print $5}' | sed -e 's/[\/\% ]/,/g'`