I wish to prepend a timestamp to each line of output from a command. For example:
foo
bar
baz
would become
[2011-12-13 12:20:38] foo
[2011-12-13 12:21:32] bar
[2011-12-13 12:22:20] baz
…where the time being prefixed is the time at which the line was printed. How can I achieve this?
Best Answer
moreutils includes
ts
which does this quite nicely:command | ts '[%Y-%m-%d %H:%M:%S]'
It eliminates the need for a loop too, every line of output will have a timestamp put on it.
You want to know when that server came back up you restarted? Just run
ping | ts
, problem solved :D.