关于unix:如何从ping结果中获取数字?

How to fetch numbers from ping result?

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

1
2
3
4
5
6
7
8
9
10
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,min 8.45,avg 8.64和max 8.72

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

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


我会带你走大部分路。

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

这将净你:

1
2
5,5,0,
8.54,8.64,8.72,0.073

从这里你只需要将它分配给bash中的变量并按照你认为合适的方式操作它:

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