Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
up vote 3 down vote accepted

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

while(<>) {
    print localtime . ": $_";

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

tail -f outputfile | ./

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

You must log in to answer this question.

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