Piping into moreutils’ ts with nanosecond precision

datetimestamps

On this system:

$ cat /etc/issue
Ubuntu 14.04.4 LTS \n \l
$ uname -a
Linux mypc 3.19.0-56-generic #62~14.04.1-Ubuntu SMP Fri Mar 11 11:03:33 UTC 2016 i686 i686 i686 GNU/Linux
$ apt-show-versions -r moreutils
liblist-moreutils-perl:i386/trusty 0.33-1build3 uptodate
moreutils:i386/trusty 0.50 uptodate

… I can use date with a nanosecond precision, no problem:

$ date +'%Y%m%d%H%M%S'
20160327133441
$ date +'%Y%m%d%H%M%S%N'
20160327133446582969969

… however, if I use the same format string with moreutils' ts, the nanosecond precision fails:

$ ping google.com | ts '%Y%m%d%H%M%S%N'
20160327133829%N PING google.com (216.58.209.110) 56(84) bytes of data.
20160327133829%N 64 bytes from arn06s07-in-f14.1e100.net (216.58.209.110): icmp_seq=1 ttl=54 time=32.0 ms
20160327133830%N 64 bytes from arn06s07-in-f14.1e100.net (216.58.209.110): icmp_seq=2 ttl=54 time=31.6 ms
^C

Any way to get ts to show nano- (or micro-) second precision when prepending timestamps to stdin?

Best Answer

Starting from moreutils 0.31 %.S specifier is available, use it instead of %S :

ping google.com | ts '%Y%m%d-%H:%M:%.S'
20160327-15:01:11.361885 PING google.com (216.58.209.206) 56(84) bytes of data.
20160327-15:01:11.362056 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=1 ttl=57 time=26.3 ms
20160327-15:01:12.314243 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=2 ttl=57 time=26.2 ms
20160327-15:01:13.315651 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=3 ttl=57 time=26.3 ms
Related Question