How to you get ping to print human readable timestamps

pingscripting

Ping can print you a timestamp on every line but its in unix date format 🙁

ping -D localhost  
PING localhost.localdomain (127.0.0.1) 56(84) bytes of data.
[1415629479.482938] 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.057 ms

I'm looking for a simple pipe command that can convert them on the fly to something like this:

[Sat 14 Feb 2009 01:31:30 SAST] 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.057 ms

Also I want it to run continuously, it shouldn't wait for the command to terminate before printing results.

Best Answer

From : https://stackoverflow.com/questions/14036116/convert-timestamp-to-datetime-with-sed

with bit of scripting...

ping -D localhost | while read row 
do 
  awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' <<< "$row"
done

Runs as

$ ping -D localhost | while read row; do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' <<< "$row" ; done
PING localhost (127.0.0.1) 56(84) bytes of data.
[2014-11-10 16:06:40.145811] 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.045 ms
[2014-11-10 16:06:41.144926] 64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.040 ms
Related Question