icz
icz

Reputation: 547

Extracting ping times and prefixing with time stamp

I am trying to extract the ping times in a more data-friendly format. My goal is to turn something like this:

64 bytes from arn06s02-in-f0.1e100.net (173.194.32.32): icmp_seq=59 ttl=54 time=31.8 ms

into something like:

1361999357 31.8

Where the first number comes from $(date +%s) and the second number is the 8th column from the ping command.

I would like to be able to run this for a long time and get a long, two-column list of timestamps and ping times.

Timestamp I have the timestamp working with the following:

ping google.com | while read line; do echo "$(date +%s) $line"; done

But when I try to add sed, awk, or cut into the pipeline to get just the time I end up with no output!

I'm not very familiar with sed or awk, though I'm certain they must be the right tools for the job. My attempts have resulted in no output.. I think it is because awk is expecting an EOF before passing the output to the next piped program?

Thanks!

Upvotes: 3

Views: 3213

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185073

Using :

$ ping google.com | awk -F'[ =]' 'NR>1{print system("echo -n $(date +%s)"), $11}'

Short & efficient, no ? =) And no buffering problems occurs...

Note : system("") can be used as a hack to avoid buffering.

Upvotes: 4

William
William

Reputation: 4935

Just to be different...

( date '+%s' ; ping -c 1 google.com; ) | sed -ne '/^[0-9][0-9]*$/h;/ bytes from /{s/^.*time=\([0-9.]*\).*/ \1/;H;g;s/\n//;p;}'

Result: 1362002815 7.52

Upvotes: 1

Dave S.
Dave S.

Reputation: 6419

Try this:

ping -c 1 google.com | grep "bytes from" | while read line; do echo "$(date +%s) $line"; done | awk '{print $1 " " $8}' | sed 's,time=,,'

Note that if you leave off the '-c', there's a certain amount of buffering that happens before you get any output. Leaving the '-c' lets you see the results more quickly and to verify that things are working.

Upvotes: 1

Related Questions