[20190502]给显示输出加入时间戳.txt
[20190502]给显示输出加入时间戳.txt
--//有别人问我执行脚本中timestamp.pl的代码,实际上有些文章里面有源代码,有一些忘记写上了。
--//贴上:
$ cat /usr/local/bin/timestamp.pl
#!/usr/bin/perl
while (<>) {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("%02d:%02d:%02d", $hour, $min, $sec);
print ": $_";
#print localtime() . ": $_";
}
--//使用timestamp.pl在开始标注时间.这样更加清晰.我好像改过,实际上这个很容易自己写一个。
$ cat ts.sh
#! /bin/bash
while read i
do
echo $(date '+%h:%m:%s') : $i
done
--//这是我临时想到的脚本,看了链接:https://serverfault.com/questions/310098/how-to-add-a-timestamp-to-bash-script-log
--//真心佩服老外,人家还考虑执行效率.
gawk '{ print strftime("[%y-%m-%d %h:%m:%s]"), $0 }'
while true; do printf '%(%f %t)t\n'; done
--//顺便在我的笔记本上测试看看.我使用cygwin64 terminal for windows:
$ yes | gawk '{ print strftime("[%y-%m-%d %h:%m:%s]"), $0 }' | uniq -c
180224 [2019-05-03 20:23:53] y
433126 [2019-05-03 20:23:54] y
430354 [2019-05-03 20:23:55] y
430532 [2019-05-03 20:23:56] y
428690 [2019-05-03 20:23:57] y
432775 [2019-05-03 20:23:58] y
$ yes |while read i; do printf '%(%f %t)t';echo " $i" ; done | uniq -c
1406 2019-05-03 20:29:13 y
12101 2019-05-03 20:29:14 y
12080 2019-05-03 20:29:15 y
12111 2019-05-03 20:29:16 y
12048 2019-05-03 20:29:17 y
12373 2019-05-03 20:29:18 y
12350 2019-05-03 20:29:19 y
$ yes |while read i; do echo $(date '+%h:%m:%s') " $i"; done | uniq -c
6 20:32:29 y
33 20:32:30 y
31 20:32:31 y
30 20:32:32 y
31 20:32:33 y
33 20:32:34 y
33 20:32:35 y
33 20:32:36 y
$ yes | xargs -i{} date "+%h:%m:%s : {}" | uniq -c
31 20:45:22 : y
31 20:45:23 : y
35 20:45:24 : y
34 20:45:25 : y
35 20:45:26 : y
35 20:45:27 : y
34 20:45:28 : y
--//实际上还有1个现成的ts命令(我没有找到,不知道在那个rpm包里面)以及perl脚本的情况.上班测试看看.
$ yes | timestamp.pl | uniq -c
267209 08:56:02: y
308591 08:56:03: y
308820 08:56:04: y
308579 08:56:05: y
308996 08:56:06: y
282290 08:56:07: y
304223 08:56:08: y
$ yes | gawk '{ print strftime("[%y-%m-%d %h:%m:%s]"), $0 }' | uniq -c
190537 [2019-05-05 08:56:58] y
516917 [2019-05-05 08:56:59] y
518052 [2019-05-05 08:57:00] y
517918 [2019-05-05 08:57:01] y
518543 [2019-05-05 08:57:02] y
517913 [2019-05-05 08:57:03] y
--//$ yes |while read i; do printf '%(%f %t)t';echo " $i" ; done | uniq -c在我的linux 5.9不支持.在rhel7 测试,显示的时间是:
1970-01-01 08:00:00 y
1970-01-01 08:00:00 y
--//有问题.
$ yes |while read i; do echo $(date '+%h:%m:%s') " $i"; done | uniq -c
210 09:00:26 y
435 09:00:27 y
433 09:00:28 y
438 09:00:29 y
439 09:00:30 y
$ yes | xargs -i{} date "+%h:%m:%s : {}" | uniq -c
223 09:00:49 : y
803 09:00:50 : y
803 09:00:51 : y
798 09:00:52 : y
1018 09:00:53 : y
814 09:00:54 : y
812 09:00:55 : y
--//google还找到如下链接:
https://unix.stackexchange.com/questions/26728/prepending-a-timestamp-to-each-line-of-output-from-a-command
firstly, if you are expecting these timestamps to actually represent an event, bear in mind that since many programs
perform line buffering (some more aggressively than others), it is important to think of this as close to the time that
the original line would have been printed rather than a timestamp of an action taking place.
you may also want to check that your command doesn't already have an inbuilt feature dedicated to doing this. as an
example, ping -d exists in some ping versions, and prints the time since the unix epoch before each line. if your
command does not contain its own method, however, there are a few methods and tools that can be employed, amongst
others:
posix shell
bear in mind that since many shells store their strings internally as cstrings, if the input contains the null character
(\0), it may cause the line to end prematurely.
command | while ifs= read -r line; do printf '[%s] %s\n' "$(date '+%y-%m-%d %h:%m:%s')" "$line"; done
gnu awk
command | gawk '{ print strftime("[%y-%m-%d %h:%m:%s]"), $0 }'
perl
command | perl -pe 'use posix strftime; print strftime "[%y-%m-%d %h:%m:%s] ", localtime'
python
command | python -c 'import sys,time;sys.stdout.write("".join(( " ".join((time.strftime("[%y-%m-%d %h:%m:%s]", time.localtime()), line)) for line in sys.stdin )))'
ruby
command | ruby -pe 'print time.now.strftime("[%y-%m-%d %h:%m:%s] ")'
--//python,ruby(没安装)没有测试,其它测试如下:
$ yes | perl -pe 'use posix strftime; print strftime "[%y-%m-%d %h:%m:%s] ", localtime'| uniq -c
10259 [2019-05-05 09:02:30] y
140363 [2019-05-05 09:02:31] y
144397 [2019-05-05 09:02:32] y
144285 [2019-05-05 09:02:33] y
131107 [2019-05-05 09:02:34] y
$ yes | while ifs= read -r line; do printf '[%s] %s\n' "$(date '+%y-%m-%d %h:%m:%s')" "$line"; done|uniq -c
50 [2019-05-05 09:04:09] y
410 [2019-05-05 09:04:10] y
400 [2019-05-05 09:04:11] y
400 [2019-05-05 09:04:12] y
--//从测试看awk脚本效率最高,使用这个脚本主要目的看某些步骤的执行时间间隔.例子:
$ ping -c 3 -i 2 192.168.100.40 | ts.awk
[2019-05-05 09:08:36] ping 192.168.100.40 (192.168.100.40) 56(84) bytes of data.
[2019-05-05 09:08:36] 64 bytes from 192.168.100.40: icmp_seq=1 ttl=64 time=0.855 ms
[2019-05-05 09:08:38] 64 bytes from 192.168.100.40: icmp_seq=2 ttl=64 time=0.094 ms
[2019-05-05 09:08:40] 64 bytes from 192.168.100.40: icmp_seq=3 ttl=64 time=0.165 ms
[2019-05-05 09:08:40]
[2019-05-05 09:08:40] --- 192.168.100.40 ping statistics ---
[2019-05-05 09:08:40] 3 packets transmitted, 3 received, 0% packet loss, time 4000ms
[2019-05-05 09:08:40] rtt min/avg/max/mdev = 0.094/0.371/0.855/0.343 ms
--//顺便问一下,那位知道ts这个命令在那个rpm安装包里面.那位知道?遇到这样的情况如何查询确定安装包.
上一篇: 怎样补充蛋白质最快,小编带你了解一下
下一篇: 喜力啤酒价格如何?喜力啤酒的特点有哪些?