How to extract info from pings.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract info from pings.?
# 1  
Old 10-05-2014
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.
Code:
# to get top 3
ping -c10 google.com | cut -d " " -f 7 | cut -d '=' -f2 | sort -rn | head -n 3

Code:
 #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?
# 2  
Old 10-05-2014
Quote:
Originally Posted by acoding
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.
Code:
# to get top 3
ping -c10 google.com | cut -d " " -f 7 | cut -d '=' -f2 | sort -rn | head -n 3

Code:
 #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.
Code:
[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.
Code:
awk -F"time=" 'NR==2 || NR==3 || NR==4 {gsub(/ms/,X,$2);print $2}' file44

Output will be as follows.
Code:
83.6 
73.0 
65.1

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-05-2014 at 07:55 AM..
# 3  
Old 10-05-2014
@OP: that is because the cut isn't filtering out the other lines..

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

or
Code:
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


---
Quote:
Originally Posted by RavinderSingh13
[..]
Code is as follows.
Code:
awk -F"time=" 'NR==2 || NR==3 || NR==4 {gsub(/ms/,X,$2);print $2}' file44

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

Last edited by Scrutinizer; 10-05-2014 at 10:55 AM.. Reason: Quote added, thanks Akshay
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-05-2014
The pingoutput is surrounded by statistical et al. information which you will have to eliminate. By sorting at the beginning, you get those listed last. Try
Code:
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.
# 5  
Old 10-05-2014
Quote:
Originally Posted by Scrutinizer
@OP: that is because the cut isn't filtering out the other lines..

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

or
Code:
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
@Scrutinizer : Hi there is one typo, you need to edit your post, a single quote need to come before pipe Smilie

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

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 10-05-2014
Thanks for the input guys. I also came up with this, its probably not the most efficient but seems to work.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk to extract additional info

Hi I use multipath linux command to get LUNs info and find out if any failed. # multipath -ll >/tmp/mpfail # cat /tmp/mpfail multipath.conf line 109, invalid keyword: user_friendly_names multipath.conf line 153, invalid keyword: user_friendly_names multipath.conf line 193, invalid... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

Extract info from sar output

Hi I have an output of sarcommand which is as follows: 10:22:18 bread/s lread/s %rcache bwrit/s lwrit/s %wcache pread/s pwrit/s 10:23:18 0 398 100 5 13 64 0 0 10:24:18 0 332 100 5 15 65 0 0 10:25:18 0 ... (7 Replies)
Discussion started by: fretagi
7 Replies

3. Shell Programming and Scripting

Compare two files and extract info

Hello, I have two files which look like this File 1 Name test1 status P Gene1 0.00236753 1 1.00E-01 Gene2 0.134187 2 2.00E-01 Gene3 0.000608716 2 3.00E-01 Gene4 0.0016234 1 4.00E-01 Gene5 0.000665868 2 5.00E-01and file 2 No Pos ... (2 Replies)
Discussion started by: nans
2 Replies

4. UNIX for Advanced & Expert Users

How to extract info from text file between the tags

Hi, I have a text file with member information... B]Name is in H1 tag Title is in H2 tag Email is in <a id="ctl00_ContentPlaceHolder3_repeaterItems_ctl01_lbnEmailMe" href="javascript:__doPostBack('ctl00$ContentPlaceHolder3$repeaterItems$ctl01$lbnEmailMe','')">someone@company.com</a> Location:... (6 Replies)
Discussion started by: igurv
6 Replies

5. Shell Programming and Scripting

how to extract info from a file using awk

Dear all I have a file call interfaces.txt Filename: interfaces.txt How can I extract the information at below? ABC_DB_001 hostname1 20901 ABC_DB_002 hostname2 20903 ABC_DB_003 hostname3 20905 Currently I am using a very stupid method grep ^ABC interfaces.txt > name.txt grep... (3 Replies)
Discussion started by: on9west
3 Replies

6. Shell Programming and Scripting

Extract info from csv

I have some input file, which contains some lines which are comma separated. Eg. a,b,id=999],d d,f,g,id=345],x x,y,x,s,id=677],y I run a loop to read the lines one by one. What i want is to extract the value on the right of id=. I cannot do it by Awk, since the column number is not fixed.... (5 Replies)
Discussion started by: indianjassi
5 Replies

7. IP Networking

How to extract NIC and other info ,given Ip

Hi all, I am working on a networking project that requires me to find out the NIC on that particular machine and many more things. Now Given the IP and the subnet. I would like to know how we can extract such informations? I am talking in exclusivity to Solaris boxes!! The required... (6 Replies)
Discussion started by: wrapster
6 Replies

8. AIX

Extract info

Anyone have a better idea to automate extraction of info like ... "uname" "ifconfig" "ps efl" "netstat -ao" etc. from several hundred aix, solaris, red hat boxes? without logging into each box and manually performing these tasks and dumping them to individual files? thanks for any input (1 Reply)
Discussion started by: chm0dvii
1 Replies

9. AIX

need to extract info from log files

hi guys i need to extract information from log files generated by an application. log file has the following lines for each process.. ---------------------------------------------- Fri Aug 03 12:06:43 WST 2007 INFO: Running project PROJECT1 Fri Aug 03 12:06:43 WST 2007 INFO: Source Files... (7 Replies)
Discussion started by: kirantalla
7 Replies

10. UNIX for Dummies Questions & Answers

using cut to extract info

a simple question, how can i use cut (after using grep) to extract the last four digits on a line. so say i had a string http://blabla:9020, how would I extract the port?? -Fez (4 Replies)
Discussion started by: hafhaq
4 Replies
Login or Register to Ask a Question