bash, help with stdout manipulation.

Hey all, Im kind of lost on how to do what I want so I figured I would ask.

I want to pipe STDOUT of an app to a log file, but I want to prepend each line of that output with the date and time.

Im drawing a complete blank on how to do this?? Any ideas?

i.e.

output is currently this:

app is running fine.
synching db files.
corruption found.
blah blah.

to look like this:

01/01/2009 13:20 - app is running fine.
01/02/2009 05:50 - synching db files.
01/09/2009 22:20 - corruption found.
02/02/2009 00:01 - blah blah.

Hi.

You will get many suggestions I think, but here's one using awk!!

cat file1 | awk '{"date" | getline A; print A, $0}' > logfile

Tue Oct  6 00:09:42 UTC 2009 app is running fine.
Tue Oct  6 00:09:42 UTC 2009 synching db files.
Tue Oct  6 00:09:42 UTC 2009 corruption found.
Tue Oct  6 00:09:42 UTC 2009 blah blah.

(I put the output you gave into a file (file1) and cat'd it to "simulate" the output of your program. Change the date command to give the format of date you desire)

app| while read line; do 
  echo $(date '+%m/%d/%Y %H:%M') $line
done> app.log

Replace > with >> if you want the append to the logfile

These look good... these are the little tricks I need to get better at this junk. Thanks to both of you for the examples.

cmd |
 while IFS= read -r line
 do
  date "+%m/%d/%Y %H:%M $line"
 done > logfile
./my_app | xargs -I{} echo $(date "+%m/%d/%Y %H:%M") - {} | tee my_log

Thanks, I learned two things, that this is a way to preserved leading and trailing spacing on a line and that you do not need a \ after a | . :b:

awk '{$0= Date FS $0}1' Date=`date "+%m/%d/%Y %H:%M"` file > newfile

scottn watch for UUOC's :wink:

Hi danmero.

The question was how to format the output of an application to include the date in the output, not just to format an input file like this.

The cat in my original post was to "simulate" the output of the application, not to cat a file as such.

Having said that, I've won a few UUOC awards in my time, for sure!

Ops, I get the point now. Sorry :wink: