Redirection after prepending timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirection after prepending timestamp
# 1  
Old 03-10-2011
Redirection after prepending timestamp

Hi all,
I have little experience with Scripting so hoping someone may be able to help me or point me in the right direction.
I have a shell script which was outputting uncaught exceptions to a log file.
$JAVA_MAIN_CLASS > $LOGNAME 2>&1

What I want to be able to do is prepend a timestamp on each entry to the log file. I updated the script as follows:
$JAVA_MAIN_CLASS | perl -pne 'print scalar(localtime()), " ";' > $LOGNAME 2>&1

But nothing gets outputted to the file until the process is terminated and at that there is information missing. It is hard to tell if it is standard out or standard error. I know the perl timestamp part works as I have tested. But its when I try to pipe it to the file I am doing something wrong. Smilie

If anyone has any ideas I would appreciate them.
# 2  
Old 03-10-2011
That's not how pipes work. Unless perl reads in the log entry in the first place it can't print it back out.

Try:
Code:
$JAVA_MAIN_CLASS | while read LINE
do
        perl -pne 'print scalar(localtime()), " ";'
        echo "$LINE"
done > logfile

There's probably a snappier and perl-less solution but without knowing what your system and shell is I can't know what date utilities, version of awk, etc. you have available.
# 3  
Old 03-10-2011
Thanks Corona688.
I had just figured out that it was the pipe that was causing the error only standard out was getting through, standard error was getting lost.
Sorry if this sounds dumb but how can I find out
"what date utilities, version of awk, etc.you have available."
# 4  
Old 03-10-2011
It wasn't quite the pipe that was doing it, it was that you were piping it into a program that didn't use it, so the data just stopped there.

I just realized you wanted stderr and stdout. slight change:
Code:
$JAVA_MAIN_CLASS 2>&1 | while read LINE
do
        perl -pne 'print scalar(localtime()), " ";'
        echo "$LINE"
done > logfile

Quote:
Sorry if this sounds dumb but how can I find out
"what date utilities, version of awk, etc.you have available."
Please tell me what your system is, and tell me what your shell is. Smilie That'll be enough to find out most of the rest.
# 5  
Old 03-10-2011
Systen is SunOS 5.10 Generic_142909-17 sun4u sparc SUNW,Sun-Fire-V240
Would I be better off trying to use awk rather than perl? As I had tried (not very hard) with awk but couldnt get it to work so I then just used perl.
# 6  
Old 03-10-2011
GNU awk and GNU date have extensions to make timestamps easy but you don't have them. Perl seems to work.

Does the above script do what you want? Or did you only want timestamps on stdout, or stderr, not both?
# 7  
Old 03-10-2011
I want timestamp on both. I have yet to try with your improved solution. Can I ask why why I need to echo "$LINE" ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

3. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

4. Shell Programming and Scripting

Sed script for appending & prepending

Hello Mates I am trying to write a script, which appends and prepends the text in a file. I tried testing with a small file and it worked fine. but for the large file, the script is wiping the entire file and adds only the word to be appended in the file. mv $file_name $file_name.bak sed... (6 Replies)
Discussion started by: sathyaac
6 Replies

5. Shell Programming and Scripting

[Shell/Perl(?)] Prepending timestamps to console output & writing results to a file

I do a lot of TSM work and I embarked on what I thought would be an easy task, and I'd be very happy for any input to save the pounding my keyboard is receiving :] By default, the output of TSM's console has no timestamping, making it hard to sort through accurately. This puts my console into... (5 Replies)
Discussion started by: Vryali
5 Replies

6. UNIX for Dummies Questions & Answers

Prepending lines with file name

I have a number of dat-files, such as abc.dat, def.dat etc, as follows: 2011-07-01 100.0 2011-07-02 101.0 2011-07-03 101.7 I want to prepend the file with the base file name; so for abc.dat it would result in the following: abc 2011-07-01 100.0 abc 2011-07-02 101.0 abc 2011-07-03 ... (5 Replies)
Discussion started by: figaro
5 Replies

7. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

8. UNIX for Dummies Questions & Answers

Prepending information from filenames into files

I would like to know how to take information from a filename and place it into the text of the same file. Let's say I have a file called height_2_width_1.txt containing data that is related to a height of 2 and a width of 1, and the text originally looks like this: where these two columns... (13 Replies)
Discussion started by: Scatterbrain26
13 Replies

9. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

10. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies
Login or Register to Ask a Question