Reputation: 6209
I have an infinite stream of data coming out of a logger, which I am piping to grep. I would like to save the output of the grep to a file, but also include a timestamp at the beginning of each line (the time at which the line appeared). Is there an easy way to accomplish this? Assume I cannot change the output of the logger process.
Upvotes: 8
Views: 8557
Reputation: 54402
You can append a static timestamp using sed
and date
:
... | sed "s/^/$(date) /" >> output.txt
Alternatively, if you require a realtime timestamp, use gawk
's strftime function:
... | gawk '{ print strftime(), $0 }'
You can define your favourite formatting:
... | gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }'
And if buffering is a problem, don't forget to flush each line:
... | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush() }'
Alternatively, use unbuffer
:
unbuffer ... | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }'
If you don't have gawk
, you have a couple of other options:
(a) Install ts
(from moreutils):
... | ts '%F %T'
(b) Use perl
:
... | perl -pe 's/^/localtime . " "/e'
or with formatting:
... | perl -MPOSIX -pe 's/^/strftime("%Y-%m-%d %H:%M:%S", localtime) . " "/e'
Don't forget that you can use gmtime
instead of localtime
if you need GMT formatted to your locale.
(c) Ask a question.
Upvotes: 17