How to extract info from pings.?

Hi guys, new to this forum. I am currently trying to extract the times from pinging a domain and list the top 3 and then also do the opposite i.e. list the bottom 3.

so if I had this as a ping result (the bold part is what I want):

64 bytes from 193.120.166.90: icmp_seq=10 ttl=128 time=34.8 ms

At the moment this is what I have.

# to get top 3
ping -c10 google.com | cut -d " " -f 7 | cut -d '=' -f2 | sort -rn | head -n 3
 #to get bottom 3
ping -c10 google.com | cut -d " " -f 7 | cut -d '=' -f2 | sort -rn | tail -n 3

The first 1 seems to work but the second dont- rather than just given me the times it gives these words "packet data", any ideas to why this is?

Hello aconding,

Welcome to forum. Following may help you in same.
Lets say we have command ping output in a file named file44 as follows.

[singh@localhost awk_programming]$ cat file44
PING google.com (74.125.236.33) 56(84) bytes of data.
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=1 ttl=128 time=83.6 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=2 ttl=128 time=73.0 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=3 ttl=128 time=65.1 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=4 ttl=128 time=62.9 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=5 ttl=128 time=64.0 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=6 ttl=128 time=64.1 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=7 ttl=128 time=70.7 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=8 ttl=128 time=55.3 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=9 ttl=128 time=73.2 ms
64 bytes from maa03s04-in-f1.1e100.net (74.125.236.33): icmp_seq=10 ttl=128 time=68.4 ms

--- google.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9085ms
rtt min/avg/max/mdev = 55.312/68.080/83.618/7.287 ms

Code is as follows.

awk -F"time=" 'NR==2 || NR==3 || NR==4 {gsub(/ms/,X,$2);print $2}' file44

Output will be as follows.

83.6 
73.0 
65.1 

Thanks,
R. Singh

@OP: that is because the cut isn't filtering out the other lines..

Try:

ping -c10 google.com | sed -n 's/.*time=\(.*\) ms$/\1/p' | sort -rn 

or

ping -c10 google.com | awk -F'time=| ms' 'NF==3{print $(NF-1)}' | sort -rn' 

You can also use sort -n and use head or tail where appropriate

---

That will render the first 3 times rather than the top 3 or the bottom 3

1 Like

The ping output is surrounded by statistical et al. information which you will have to eliminate. By sorting at the beginning, you get those listed last. Try

ping -c10 google.com | sort -t= -k4,4rn |
  awk   '               # {print > "DBG"}  # use for debug/control purposes only 
         !NF            {exit}
                        {split ($(NF-1), T, "=")
                         CB[(++CNT)%3]=T[2]
                        }
         SLOW && NR==3  {exit}
         END            {for (i=1; i<=3; i++) print CB[(++CNT)%3]}       
        ' SLOW="0"      # ; cat DBG  #      use for debug/control purposes only 

Set SLOW to 1 for slowest responses (I assume that's your "bottom"s), and to 0 for the fastest.

@Scrutinizer : Hi there is one typo, you need to edit your post, a single quote need to come before pipe :slight_smile:

ping -c10 google.com | awk -F'time=| ms' 'NF==3{print $(NF-1)}' | sort -rn 
1 Like

Thanks for the input guys. I also came up with this, its probably not the most efficient but seems to work.

ping -c10 google.com | awk '/time=/  { print $7 }' | cut -d '=' -f2 | sort -rn | tail -n 3