Firstly, if you are expecting these timestamps to actually represent an event, bear in mind that since many programs perform line buffering (some more aggressively than others), it is important to think of this as close to the time that the original line would have been printed rather than a timestamp of an action taking place.
You may also want to check that your command doesn't already have an inbuilt feature dedicated to doing this. As an example, ping -D
exists in some ping
versions, and prints the time since the Unix epoch before each line. If your command does not contain its own method, however, there are a few methods and tools that can be employed, amongst others:
POSIX shell
Bear in mind that since many shells store their strings internally as cstrings, if the input contains the null character (\0
), it may cause the line to end prematurely.
command | while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
GNU awk
command | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
Perl
command | perl -pe 'use POSIX strftime; print strftime "[%Y-%m-%d %H:%M:%S] ", localtime'
Python
command | python -c 'import sys,time;sys.stdout.write("".join(( " ".join((time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime()), line)) for line in sys.stdin )))'
Ruby
command | ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")'