0

I aim to produce an output which can be used to develop an average based on 5 server ping's. I'm running:

ping 8.8.8.8 -c 5 > ping.txt

which outputs

PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=46 time=29.331 ms
64 bytes from 8.8.8.8: seq=1 ttl=46 time=34.057 ms
64 bytes from 8.8.8.8: seq=2 ttl=46 time=33.978 ms
64 bytes from 8.8.8.8: seq=3 ttl=46 time=33.629 ms
64 bytes from 8.8.8.8: seq=4 ttl=46 time=48.414 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 29.331/35.881/48.414 ms

Then, I aim to remove the textual data surrounding the time in ms for each ping to the server, at the moment, I have:

sed 's/".*time=//' ping.txt > pingres.txt

Which doesn't quite remove all the data I require and outputs as follows:

PING 8.8.8.8 (8.8.8.8): 56 data bytes
29.331 ms
34.057 ms
33.978 ms
33.629 ms
48.414 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 29.331/35.881/48.414 ms

I would like to show the data in the following way, but have forgotten some principles of sed to achieve this and am quite the novice..

29.331
34.057
33.978
33.629
48.414

I realize, there's already an average marked, at the bottom of the file, but I require this method for other reasons

  • @Theophrastus I can't use perl on the platform I'm running unfortunately – Ballard Mar 27 '17 at 20:41
1

You can do a bit more filtering with grep to limit the lines you're looking at:

$ ping 127.0.0.1 -c 5 | grep "bytes from" | sed -n -e 's/^.*time=//' -n -e 's/[^0-9\.].*$//p'
0.020
0.040
0.054
0.036
0.049
  • also removes the need for me to use two files, thanks pal – Ballard Mar 27 '17 at 20:38
1
grep -m5 -oP '\stime=\K\d+\.\d+(?=\s+ms$)' ping.text

sed -ne '2,6s/.* time=\([^ ]*\) ms/\1/p' ping.text

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.