Reputation: 302
The output of a typical ping command is -
--- 192.168.1.107 ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 1008ms
rtt min/avg/max/mdev = 0.288/0.288/0.288/0.000 ms
I want to display only "50% packet loss" portion on a terminal window when I run the ping command in a shell script. How should I proceed for it ?
Upvotes: 1
Views: 1351
Reputation: 2383
Using grep:
ping -c10 -f -q localhost | grep -E -o '[^[:space:]]+ packet loss'
Using awk:
ping -c10 -f -q localhost | awk -F', ' '/packet loss/ { print $3 }'
grep -o isn't posix, while the awk solution depends on the output format.
Upvotes: 1
Reputation: 113994
-o
tells grep
to print only the matching portion:
ping -c2 -q targethost | grep -o '[^ ]\+% packet loss'
If the output of ping is viewed as comma-separated fields, then, as shown in your sample output, the packet loss info is in the third field. This allows us to use awk as follows:
ping -c2 -q targethost | awk -F, '/packet loss/{print $3;}'
Upvotes: 1