0

So I am trying to write shell script in linux that will extract the rountrip time from a webserver ping stored in a text file. So what I basically have is a text file with this:

    PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
    ... (a bunch more ping responses here)
    --- e11699.b.akamaiedge.net ping statistics ---
    86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
    rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

So I ended up using sed to try and extract only the 17.2, 12.6, 11.7, and more times from the text file. below is my sed line:

    sed 's/.*(time=\([0-9]*\(\.[0-9]*\)\{0,1\}\) ms/\1/' pingoutput.txt | sort -n > sortedtime.txt 

This line successfully extracted and sorted all the times I needed BUT it also extracted the few lines from the ping text file i didnt need. The text file it had created looked like this:

--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2
11.7
12.6
17.2
...
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms

If there is anyway to avoid extracted the unwanted "---e11699" to "pipe 2" and the "86400 packets" to "86532481ms" lines of the text file I would really appreciate the help!

0

I used this sed:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n

On your example file (times):

PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
... (a bunch more ping responses here)
--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

I get this result:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n
11.7
12.6
17.2


I used the -n switch to get rid of unwanted lines. From man sed:

-n By default, each line of input is echoed to the standard output after all of the commands have been applied to it. The -n option suppresses this behavior.

  • That worked perfectly! Thank you very much! – user192314 Oct 3 '16 at 12:42
  • @user192314 My pleasure. Glad you were able to use it! – maulinglawns Oct 3 '16 at 12:43
  • Would you be able to write the equivalent statement in python code instead of shell script? – user192314 Oct 3 '16 at 18:21
  • @user192314 Yes, I could. – maulinglawns Oct 3 '16 at 18:22
  • can you add it in as another answer – user192314 Oct 3 '16 at 18:34
0

if grep with pcre regex is okay:

$ grep -oP 'time=\K[0-9.]+' ip.txt | sort -n
11.7
12.6
17.2
  • -o print only matching pattern
  • -P use pcre regex
  • time=\K positive lookbehind, not part of output
  • [0-9.]+ one or more of digits and . characters
  • sort -n sort numerically

with perl alone:

$ perl -nle 'push(@a,/time=\K([0-9.]+)/g); END{ print foreach sort {$a <=> $b} @a }' ip.txt 
11.7
12.6
17.2
  • here an array is populated with matching patterns and then at end, numerically sorted array is printed out
-1
grep 'time=' pingoutput.txt | awk '{print $8}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.