Perl の場合:
command 2>&1 | perl -pe 'print scalar(localtime()), " ";'
gawk を使用:
command 2>&1 | awk '{ print strftime(), $0; fflush() }'
command
を置き換えます tail -f logfile
で あなたの特定の例のために。または、元のプログラムの stdout/stderr を上記のパイプにリダイレクトすることもできます。
試す
tail -f logfile | while read line; do echo `date` "$line" ; done
これを試すことができます
cat /etc/motd | xargs -d"\n" -I {} date +"%Y-%m-%d %H:%M:%S {}"
出力例:
2013-02-26 15:13:57 2013-02-26 15:13:57 The programs included with the Debian GNU/Linux system are free software; 2013-02-26 15:13:57 the exact distribution terms for each program are described in the 2013-02-26 15:13:57 individual files in /usr/share/doc/*/copyright. 2013-02-26 15:13:57 2013-02-26 15:13:57 Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent 2013-02-26 15:13:57 permitted by applicable law.