20

I would like to have a timestamp printed in front of every line that comes out of a "tail -f outputfile", so that I know when is each line printed in. Something like:

[...]
20110617_070222:results printed here
20110617_070312:results printed here
20110617_070412:results printed here
20110617_070513:results printed here
[...]

Any ideas?

2
  • 1
    Note that you aren't getting quite what you asked for. The answers you've received are based upon the assumption that you don't mind the timestamp errors introduced by the fact that these are the times that tail writes to its standard output, not the times that the lines were written to the original file in the first place. You may well not mind, but be aware of this assumption derived from your question as it is written.
    – JdeBP
    Commented Jun 17, 2011 at 12:31
  • 1
    Duplicate question, with good answers: unix.stackexchange.com/q/26728/52959 Commented Apr 28, 2020 at 10:23

4 Answers 4

26

It probably doesn't get any shorter than this:

tail -f outputfile | xargs -IL date +"%Y%m%d_%H%M%S:L"

Note that I used the timestamp format suggested by your question, but you're free to use anything supported by the date command (the format syntax is supported by standard BSD and GNU date).

6
  • Note that xargs ignores empty lines. For my usecase that's fine but your milage may vary. Commented Feb 24, 2017 at 7:34
  • This will break if xargs encounters unmatched quote " in the line.
    – MarSoft
    Commented Apr 28, 2017 at 20:11
  • 1
    I see. Maybe tail -f outputfile | tr '\n' '\0' | xargs -0IL date +"%Y%m%d_%H%M%S:L" is better in this case? Also works with empty lines then. Commented Apr 28, 2017 at 23:54
  • Wouldn't this invoke date on each and every output line? Commented Oct 6, 2020 at 10:59
  • @ThorbjørnRavnAndersen That's right. But a few date invocations per second shouldn't be a problem on any system. Otherwise you probably should resort to a script or a compiled program. Or if you want to keep it pure bash you can restrict yourself to builtins, e.g.: tail -f outputfile | while read line; do printf "%(%Y%m%d_%H%M%S)T:%s\n" "$EPOCHSECONDS" "$line"; done; I didn't do any performance measurements here though. (But if you really need subsecond precision, just prefixing $EPOCHREALTIME makes more sense.) Commented Oct 7, 2020 at 8:43
6

Write a simple script to do that. Here's an example in perl:

#!/usr/bin/perl
while(<>) {
    print localtime . ": $_";
}

To use the script, simply pipe your tail output through it:

tail -f outputfile | ./prepend_timestamp.pl

You could also do it inline:

tail -f outputfile | perl -pe '$_ = localtime.": $_"'
1

I would normally do this with perl as answered by others, but it was unavailable at the rather trimmed down Linux machine where I needed it, but the busybox awk applet was!

tail -f some_file  | awk -e '{ print strftime("%Y%m%d_%H%M%S",systime()) "\t" $0}'

As always the timestamp is for when the line was printed, not when it was written originally.

0

With awk:

tail -f infile | awk '{"date \"+%Y%m%d_%H%M%S\"" | getline now} {close("date")} {print now ": " $0}'
2
  • 2
    This prints the same time for all lines in my case, even if they appear 10 seconds appart. Commented Sep 29, 2016 at 23:55
  • 2
    This prints the same time code at the front of every time, forever.
    – lbutlr
    Commented Oct 10, 2019 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .