7

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.

Tim
  • 6,079
  • 8
  • 35
  • 41
  • Duplicate question, with aswers: https://unix.stackexchange.com/q/26728/52959 – David Balažic Apr 28 '20 at 10:22
  • Does this answer your question? [Is there a Unix utility to prepend timestamps to stdin?](https://stackoverflow.com/questions/21564/is-there-a-unix-utility-to-prepend-timestamps-to-stdin) – David Balažic Apr 28 '20 at 10:25

1 Answers1

14

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.

Community
  • 1
  • 1
Steve
  • 51,466
  • 13
  • 89
  • 103