filmov
tv
Unix & Linux: Prepending a timestamp to each line of output from a command (7 Solutions!!)

Показать описание
Unix & Linux: Prepending a timestamp to each line of output from a command
The Question: 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?
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 118 people ==
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] %sn' "$(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 *****
***** Ruby *****
== This solution helped 328 people ==
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.
$ echo -e "foonbarnbaz" | ts '[%Y-%m-%d %H:%M:%S]'
[2011-12-13 22:07:03] foo
[2011-12-13 22:07:03] bar
[2011-12-13 22:07:03] baz
You want to know when that server came back up you restarted? Just run ping |
ts , problem solved :D.
== This solution helped 1 person ==
You can do this with date and xargs:
... | xargs -L 1 echo `date +'[%Y-%m-%d %H:%M:%S]'` $1
***** Explanation: *****
xargs -L 1 tells xargs to run the proceeding command for every 1 line of input,
and it passes in the first line as it does so. echo `date +'[%Y-%m-%d %H:%M:
%S]'` $1 basically echoes the date with the input argument at the end of it
The Question: 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?
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 118 people ==
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] %sn' "$(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 *****
***** Ruby *****
== This solution helped 328 people ==
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.
$ echo -e "foonbarnbaz" | ts '[%Y-%m-%d %H:%M:%S]'
[2011-12-13 22:07:03] foo
[2011-12-13 22:07:03] bar
[2011-12-13 22:07:03] baz
You want to know when that server came back up you restarted? Just run ping |
ts , problem solved :D.
== This solution helped 1 person ==
You can do this with date and xargs:
... | xargs -L 1 echo `date +'[%Y-%m-%d %H:%M:%S]'` $1
***** Explanation: *****
xargs -L 1 tells xargs to run the proceeding command for every 1 line of input,
and it passes in the first line as it does so. echo `date +'[%Y-%m-%d %H:%M:
%S]'` $1 basically echoes the date with the input argument at the end of it