在这个系统上:
$ 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
...我可以使用date
纳秒精度,没问题:
$ date +'%Y%m%d%H%M%S'
20160327133441
$ date +'%Y%m%d%H%M%S%N'
20160327133446582969969
moreutils
...但是,如果我使用与'相同的格式字符串ts
,纳秒精度将失败:
$ 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
在将时间戳添加到标准输入时,有什么方法可以ts
显示纳秒(或微秒)精度?
答案1
从 moreutils 0.31 开始%.S
可用说明符,使用它代替 %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
答案2
从 v0.62 开始,%T
格式说明符还提供了带有点名称的高分辨率版本,即%.T
:
% ( for i in {1..5}; do date +'%T.%N'; sleep 0.7; done ) | ts '[%F %.T]'
[2021-04-14 12:31:30.655183] 12:31:30.613979000
[2021-04-14 12:31:31.330801] 12:31:31.329998000
[2021-04-14 12:31:32.046576] 12:31:32.046036000
[2021-04-14 12:31:32.763263] 12:31:32.762500000
[2021-04-14 12:31:33.478184] 12:31:33.477380000