Quantcast
Viewing all articles
Browse latest Browse all 99

awkで他のコマンドの実行結果に一行ごとにタイムスタンプを付与する

個人的によく利用するので、備忘として残しておく。
pingやvmstatなどでコマンドを定期実行した際に、いつ・どのような結果になったのか残しておきたいのに、タイムスタンプを残す機能がコマンドに備わってない場合に有効だ。

やり方は簡単で、以下のように対象のコマンドとawkをパイプでつなぐだけだ。

コマンド | awk '{print strftime("%F %T ") $0}'

Image may be NSFW.
Clik here to view.
20160601_084730000000

[root@BS-PUB-CENT7-01 ~]# ping 8.8.8.8 | awk '{print strftime("%F %T ") $0}'
2016-06-01 08:47:02 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2016-06-01 08:47:02 64 bytes from 8.8.8.8: icmp_seq=1 ttl=53 time=4.34 ms
2016-06-01 08:47:03 64 bytes from 8.8.8.8: icmp_seq=2 ttl=53 time=4.60 ms
2016-06-01 08:47:04 64 bytes from 8.8.8.8: icmp_seq=3 ttl=53 time=4.67 ms
2016-06-01 08:47:05 64 bytes from 8.8.8.8: icmp_seq=4 ttl=53 time=4.43 ms
2016-06-01 08:47:06 64 bytes from 8.8.8.8: icmp_seq=5 ttl=53 time=4.74 ms
2016-06-01 08:47:07 64 bytes from 8.8.8.8: icmp_seq=6 ttl=53 time=4.54 ms
2016-06-01 08:47:08 64 bytes from 8.8.8.8: icmp_seq=7 ttl=53 time=4.74 ms
2016-06-01 08:47:09 64 bytes from 8.8.8.8: icmp_seq=8 ttl=53 time=4.31 ms
2016-06-01 08:47:10 64 bytes from 8.8.8.8: icmp_seq=9 ttl=53 time=4.24 ms

 

Image may be NSFW.
Clik here to view.
AWK実践入門 (Software Design plus)
AWK実践入門 (Software Design plus)
Image may be NSFW.
Clik here to view.
AWK実践入門 (Software Design plus)
AWK実践入門 (Software Design plus)

Viewing all articles
Browse latest Browse all 99