bash, help with stdout manipulation.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash, help with stdout manipulation.
# 1  
Old 10-06-2009
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?


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.

# 2  
Old 10-06-2009

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)
# 3  
Old 10-06-2009
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

Last edited by Scrutinizer; 10-06-2009 at 09:09 PM..
# 4  
Old 10-06-2009
These look good... these are the little tricks I need to get better at this junk. Thanks to both of you for the examples.
# 5  
Old 10-06-2009
cmd |
 while IFS= read -r line
  date "+%m/%d/%Y %H:%M $line"
 done > logfile

# 6  
Old 10-07-2009
./my_app | xargs -I{} echo $(date "+%m/%d/%Y %H:%M") - {} | tee my_log

# 7  
Old 10-10-2009
Originally Posted by cfajohnson
cmd |
 while IFS= read -r line
  date "+%m/%d/%Y %H:%M $line"
 done > logfile

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 | . Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH] Performance question - Script to STDOUT

Hello Coders Some time ago i was asking about python and bash performances, and i was told i could post the regarding code, and someone would kindly help to make it faster (if possible). If you have noted, i'm on the way to finalize, finish, stable TUI - Text(ual) User Interface. It is a... (6 Replies)
Discussion started by: sea
6 Replies

2. Shell Programming and Scripting

Mkbootfs writing to stdout in bash script

Hi, I need to automate some repacking tasks of a boot image for Android When in command line, I can use this command: mkbootfs /path/to/root > /path/to/ramdisk-recovery.cpio;However, if I try to run the command from a shell script under Ubuntu, it fails and outputs to stdout instead of the... (27 Replies)
Discussion started by: Phil3759
27 Replies

3. Shell Programming and Scripting

Bash - file manipulation

I need to change a file like this: John Smith; John Smith; Frank Olsen; Frank Olsen; Into: John Smith;; Frank... (2 Replies)
Discussion started by: locoroco
2 Replies

4. Shell Programming and Scripting

Bash array manipulation

seeking assistance on comparing two arrays using bash: array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5) array2=(disk2, disk5 vol2, vol4 ) 1) if two arrays have same elements; EXIT else populate array3 & array4 with elements that are different between array1 & array2 as:... (3 Replies)
Discussion started by: solaix14
3 Replies

5. Shell Programming and Scripting

Bash string manipulation

In my script I'm retrieving a parameter through an API call. I need to trim some things out of the result so I can use it as a parameter to pass to another process. I've got it working but it's pretty kludgy and I'm hoping someone can help me with a better way. This is the code that retrieves... (2 Replies)
Discussion started by: withanh
2 Replies

6. Shell Programming and Scripting

bash string manipulation

Hello guys, here is my problem: I got a shell script which is called by an external piece of software, the external software is not under my control. The software passes data as an argument to my script like ./ 'service;1234567890;ok;hostname;some text here' I need to pass the... (3 Replies)
Discussion started by: snoogie
3 Replies

7. UNIX for Advanced & Expert Users

string manipulation in bash shell

Hi All, I am using a bash shell and want to the following thing. A process sends the following string to my script BACKUP_FAIL_REASON="Failed - Application Dump CDMACA-0:grep: /opt/nortel/ca/data/1245184/sd00/image1/S110907070708HIS... (4 Replies)
Discussion started by: Pkumar Sachin
4 Replies

8. Shell Programming and Scripting

AWK manipulation in bash script

EDIT: This has been SOLVED. Thanks! Greetings everyone, I've posted a few threads with some quick help questions, and this is another one of those. I can't post enough gratitude for those much more knowledgeable than myself who are willing to give good advice for my minor issues. Now,... (2 Replies)
Discussion started by: Eblue562
2 Replies

9. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies

10. Shell Programming and Scripting

Bash file manipulation help

Hello, I'm writing a bash script and I have a question. Here's what I'm doing: I have a file called inv.dat which contains the following: B102:Action Figure - Teacher:79 B103:Bike - Purple:23 B104:Baseball:25 B105:Cricket Bat:15 B101:Action Figure - Fireman:15 B100:Flame-Thrower:25 ... (2 Replies)
Discussion started by: netmaster
2 Replies
Login or Register to Ask a Question