tricks

Add a timestamp to the output of a given command

Have you ever wanted to add a leading timestamp on each line of a given output?

That would make it a perfect professional syslog-like log!

Fortunately, it’s just a one-liner with gawk (the gnu version of the popular tool):

$ ./command | gawk '{print strftime("%b %d %T",systime()),$0;}'

For example, you can create a simple one-on-one IRC-like connection with:

$ nc -l -p 1234 | gawk '{print strftime("%b %d %T",systime()),$0;}'
set 16 18:25:27 Hi!
set 16 18:25:31 Are you there?
yes
set 16 18:25:41 Great!

The user you want to chat with, will have to run