Append file with grep output but add timestamp?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append file with grep output but add timestamp?
# 1  
Old 06-25-2009
Question Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file:

grep "monkey" zoo.log | tail -1 >> cron-zoo-log

Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added?

At the moment the cron-zoo-log just looks like this:

monkey house
monkey car

Could I get it to be:

monkey house 2009-01-01:16:54:00
monkey car 2009-01-01:17:01:00

Thank you.
# 2  
Old 06-25-2009
Code:
grep "monkey" zoo.log | tail -1 | sed "s/^/$(date)/" >> cron-zoo-log

# 3  
Old 06-25-2009
Quote:
Originally Posted by vgersh99
Code:
grep "monkey" zoo.log | tail -1 | sed "s/^/$(date)/" >> cron-zoo-log

Excellent.

That is putting the date before the grep result - is there a way to switch it around and maybe separate them by a comma?
# 4  
Old 06-25-2009
Quote:
Originally Posted by Sepia
Excellent.

That is putting the date before the grep result - is there a way to switch it around and maybe separate them by a comma?
sure:
Code:
grep "monkey" zoo.log | tail -1 | sed "s/$/,$(date)/" >> cron-zoo-log

I'll let you change the format of 'date' yourself.
# 5  
Old 06-25-2009
Thanks but that is now doing:

,Thu Jun 25 13:04:59 BST 2009count: 3

Where 'count 3' should have four words infront of it!

I've tried changing the order of sed "s/$/,$(date)/" around but that doesn't seem to help.

edit: doesnt seem to be writing the date when its done as a cronjob. Just has $(date),monkey

Last edited by Sepia; 06-25-2009 at 09:25 AM..
# 6  
Old 06-25-2009
Quote:
Originally Posted by Sepia
Thanks but that is now doing:

,Thu Jun 25 13:04:59 BST 2009count: 3

Where 'count 3' should have four words infront of it!
hm....... strange
try: sed "s/\$/,$(date)/"
Quote:
Originally Posted by Sepia
I've tried changing the order of sed "s/$/,$(date)/" around but that doesn't seem to help.

edit: doesnt seem to be writing the date when its done as a cronjob. Just has $(date),monkey
'crontab' runs in Bourne shell - the '$(date)' is ksh/bash specific. Change '$(date)' to '`date`' in crontab - or write your own ksh/bash wrapper and call it from cron.
# 7  
Old 06-25-2009
Quote:
Originally Posted by vgersh99
hm....... strange
try: sed "s/\$/,$(date)/"
No joy, still missing text.

Quote:
Originally Posted by vgersh99
'crontab' runs in Bourne shell - the '$(date)' is ksh/bash specific. Change '$(date)' to '`date`' in crontab - or write your own ksh/bash wrapper and call it from cron.
Thanks. Changed it to `date` and it is now displayed properly.

Thanks.
 
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

Append to a file repeating output

Hello, i'm trying to force a command to read every second from an interface watch -n1 (command) /dev/x | cat >> output but it continue to overwrite the file, without append the content Thanks and advace for help as usual regards (4 Replies)
Discussion started by: Board27
4 Replies

3. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

4. Shell Programming and Scripting

How to append timestamp in the filenames using find?

Hi, How to change the filenames with timestamp in sub folders I have the following code to select the records. find . -type f -name '*pqr*' -ctime 1 -print The following is the example app_root_dir="/`echo $ScriptDir | cut -d'/' -f2`" $app_root_dir/../BadFiles directory uvw.bad... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

append an output file with two columns

Hi All, can you help me with this: grep XXX dir/*.txt|wc -l > newfile.txt - this put the results in the newfile.txt, but I want to add another column in the newfile.txt, string 'YYYYY', separated somehow, which corresponds on the grep results? For example grep will grep XXX dir/*.txt|wc -l >... (5 Replies)
Discussion started by: apenkov
5 Replies

6. UNIX for Dummies Questions & Answers

Output to file but append rather than overwrite?

I am running a command which has a parameter that outputs the results to a file each time it is run. Here is the command: --fullresult=true > importlog.xml Can I add the output to the file rather than creating a new one which overwrites the existing one? If not can I make the file name... (2 Replies)
Discussion started by: Sepia
2 Replies

7. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the log file each line starts with timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file... (1 Reply)
Discussion started by: achu
1 Replies

8. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the logfile , lines contains timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file format.Please... (1 Reply)
Discussion started by: achu
1 Replies

9. Shell Programming and Scripting

Append Output to another file in Perl

Hi All, I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2. I tried out with the below code but couldn't work. Can any expert give me some advice? open(OUTPUT, 'perl myscript.pl file1 |'); close OUTPUT;... (7 Replies)
Discussion started by: Raynon
7 Replies

10. Shell Programming and Scripting

Append output to file

Hi, I have a script below. It get's the data from the output of a script that is running hourly. My problem is every time my script runs, it deletes the previous data and put the current data. Please see output below. What I would like to do is to have the hourly output to be appended on the... (3 Replies)
Discussion started by: ayhanne
3 Replies
Login or Register to Ask a Question