Is there a Unix utility to prepend timestamps to stdin?
Solution 1:
ts
from moreutils will prepend a timestamp to every line of input you give it. You can format it using strftime too.
$ echo 'foo bar baz' | ts
Mar 21 18:07:28 foo bar baz
$ echo 'blah blah blah' | ts '%F %T'
2012-03-21 18:07:30 blah blah blah
$
To install it:
sudo apt-get install moreutils
Solution 2:
Could try using awk
:
<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
You may need to make sure that <command>
produces line buffered output, i.e. it flushes its output stream after each line; the timestamp awk
adds will be the time that the end of the line appeared on its input pipe.
If awk shows errors, then try gawk
instead.
Solution 3:
annotate, available via that link or as annotate-output
in the Debian devscripts
package.
$ echo -e "a\nb\nc" > lines
$ annotate-output cat lines
17:00:47 I: Started cat lines
17:00:47 O: a
17:00:47 O: b
17:00:47 O: c
17:00:47 I: Finished with exitcode 0