LeonF
LeonF

Reputation: 433

How to fetch numbers from ping result?

I did a ping experiment ping -s www.google.com 56 5 on sunOS, and the result is:

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

What I need are the numbers of packets received5, packet loss0, min8.45, avg8.64 and max8.72.

I was trying to use > to store the result in a file. What I want is 5, 0, 8.45, 8.64, 8.72.

Can I use grep to do this? Do you have a better way?

Upvotes: 0

Views: 529

Answers (1)

David Hoelzer
David Hoelzer

Reputation: 16331

I'll take you most of the way.

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

This will net you:

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

From here you simply need to assign it into a variable in bash and manipulate it as you see fit:

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

Upvotes: 1

Related Questions