[20190502]給顯示輸出加入時間戳.txt

lfree發表於2019-05-05

[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


--//這是我臨時想到的指令碼,看了連結:

--//真心佩服老外,人家還考慮執行效率.

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還找到如下連結:


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安裝包裡面.那位知道?遇到這樣的情況如何查詢確定安裝包.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2643258/,如需轉載,請註明出處,否則將追究法律責任。

相關文章