Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
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 Jun 17 '11 at 12:31

2 Answers 2

up vote 3 down vote accepted

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.": $_"'
share|improve this answer

With awk:

tail -f infile | awk '{"date \"+%Y%m%d_%H%M%S\"" | getline now} {close("date")} {print now ": " $0}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.