tags:

views:

3566

answers:

8

I ended up writing a quick little script for this in Python, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, a timestamp. Ideally, the use would be something like:

$ cat somefile.txt | prepend-timestamp

(Before you answer sed, I tried this:

$ cat somefile.txt | sed "s/^/`date`/"

but that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)

Note on the accepted answer below: strftime() appears to be a GNU awk extension, so if you're on Mac OS, for example, use gawk instead of awk.

+4  A: 

How about this?

cat somefile.txt | perl -pne 'print scalar(localtime()), " ";'

Judging from your desire to get live timestamps, maybe you want to do live updating on a log file or something? Maybe

tail -f /path/to/log | perl -pne 'print scalar(localtime()), " ";' > /path/to/log-with-timestamps
jj33
This perl example was particularly useful for me as I was working as a user on an ubuntu deployment, which apparently uses 'mawk' instead of 'gawk' -- and doesn't have the strftime function. Thanks!
opello
A: 

If the value you are prepending is the same on every line, fire up emacs with the file, then:

Ctrl + <space>

at the beginning of the of the file (to mark that spot), then scroll down to the beginning of the last line (Alt + > will go to the end of file... which probably will involve the Shift key too, then Ctrl + a to go to the beginning of that line) and:

Ctrl + x r t

Which is the command to insert at the rectangle you just specified (a rectangle of 0 width).

2008-8-21 6:45PM <enter>

Or whatever you want to prepend... then you will see that text prepended to every line within the 0 width rectangle.

UPDATE: I just realized you don't want the SAME date, so this won't work... though you may be able to do this in emacs with a slightly more complicated custom macro, but still, this kind of rectangle editing is pretty nice to know about...

Mike Stone
+1  A: 

I'm not an Unix guy, but I think you can use

gawk '{print strftime("%d/%m/%y",systime()) $0 }' < somefile.txt
PabloG
+10  A: 

Could try using awk:

<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

You may need to make sure that <command> produces line buffered output, ie it flushes it's output stream after each line; the timestamp awk adds will be the time that the end of the line appeared on its input pipe.

Kieron
On my system 'awk' itself was buffering. (This can be problematic for log files). I fixed this by using: awk '{ print strftime("%Y-%m-%dT%H:%M:%S"), $0; fflush(); }'
+2  A: 

Kieron's answer is the best one so far. If you have problems because the first program is buffering its out you can use the unbuffer program:

unbuffer <command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

It's installed by default on most linux systems. If you need to build it yourself it is part of the expect package

http://expect.nist.gov

Mark Harrison
+6  A: 

annotate, available via that link or as annotate-output in the Debian devscripts package.

$ echo -e "a\nb\nc" > lines
$ annotate-output cat lines
17:00:47 I: Started cat lines
17:00:47 O: a
17:00:47 O: b
17:00:47 O: c
17:00:47 I: Finished with exitcode 0
Ted Percival
+2  A: 

Use the read(1) command to read one line at a time from standard input, then output the line prepended with the date in the format of your choosing using date(1).

$ cat timestamp
#!/bin/sh
while read line
do
  echo `date` $line
done
$ cat somefile.txt | ./timestamp
caerwyn
+1  A: 
#! /bin/sh
unbuffer "$@" | perl -e '
use Time::HiRes (gettimeofday);
while(<>) {
        ($s,$ms) = gettimeofday();
        print $s . "." . $ms . " " . $_;
}'