How to keep appending a newly created file based on some keywords


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to keep appending a newly created file based on some keywords
# 1  
Old 06-01-2009
Hammer & Screwdriver How to keep appending a newly created file based on some keywords

Hi Friends,
I have to create a new log file everyday and append it with content based on some keywords found in another log file.
Here is what I have tried so far...

grep Error /parentfolder/someLogFile.log >> /parentfolder
/Archive/"testlogfile_error_`date '+%d%m%y'`.txt"

grep error /parentfolder/someLogFile.log >> /parentfolder
/Archive/"testlogfile_error_`date '+%d%m%y'`.txt"

grep Failed /parentfolder/someLogFile.log >> /parentfolder
/Archive/"testlogfile_error_`date '+%d%m%y'`.txt"

grep failed /parentfolder/someLogFile.log >> /parentfolder
/Archive/"testlogfile_error_`date '+%d%m%y'`.txt"

Following are the instructions that I must follow:
1. Log file should either gte created in Archive folder or should get created in parentfolder and then moved to Archive.
2. Newly created log file should have date appended to its name.(I have taken care of this).
3. This file should get appended whenever there is an Error, error, Failed or failed keyword is found in "someLogFile.log" file.

The issue I am facing are
1. The new log file either do not get created properly or if created it does not get appended by the content of next found words.
2. Even if i am able to create the file it does not open at all, untill I use a touch command to create a new file and append it with first keyword.

I am not looking for complete code but if I am making any mistakes in my approch then any correction required is appriciated.
# 2  
Old 06-01-2009
The requirement to create a file before it can be appended to applies to some Unixes but not others, so in your case I would do:
Code:
DATESTR=`date '+%d%m%y'`

touch /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt"

grep Error /parentfolder/someLogFile.log >> /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt"
 
grep error /parentfolder/someLogFile.log >> /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt"
 
grep Failed /parentfolder/someLogFile.log >> /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt"
 
grep failed /parentfolder/someLogFile.log >> /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt"

The touch will simply amend the date-time stamp if the file already exists, if it does not it will create the empty file ready for appending.

The above will cause duplicate entries to appear in the /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt" file, you could do the following to remove them:
Code:
sort /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt" | uniq > /parentfolder/Archive/"testlogfile_error_${DATESTR}.sorted" && \
cp /parentfolder/Archive/"testlogfile_error_${DATESTR}.sorted" /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt" && \
rm /parentfolder/Archive/"testlogfile_error_${DATESTR}.sorted"

but the order the lines appear in /parentfolder/Archive/"testlogfile_error_${DATESTR}.txt" will be changed.
# 3  
Old 06-02-2009
when you are using sort use -u option instead of uniq
# 4  
Old 06-02-2009
Code:
newlogname=/parentfolder/Archive/testlogfile_error_`date '+%d%m%y'`.txt
touch $newlogname
egrep "Error|error|Failed|failed" /parentfolder/someLogFile.log >> $newlogname

-Devaraj Takhellambam
# 5  
Old 06-02-2009
Hi, doess the | operator works as an AND or as an OR? I need to have AND as i intend to find any or all of the keywords and append the log file.
Thanks to all experts I will give it a try today and will update you how it works.
Thanks a lot.
# 6  
Old 06-02-2009
Quote:
Originally Posted by supreet
Hi, doess the | operator works as an AND or as an OR? I need to have AND as i intend to find any or all of the keywords and append the log file.
Thanks to all experts I will give it a try today and will update you how it works.
Thanks a lot.
it works as OR
# 7  
Old 06-02-2009
Hi devtakh, Thanks a lot your code worked just fine.Smilie
Tony I am not sure why but when I run your code the file got created but when I clicked on it to open I got an error saying that "the file can not be created". Still thanks for your help. I lerned some concepts from your peace of code, which I can use elsewhere.

-----Post Update-----

I am sorry guys but even dev's code worked only for the first time. I deleted the generated log file and tried creating it again but this time I am getting the same error as "Can not create /temp/testlogfile_error_020609.txt"
Atteched is the screenshot for referance.

-----Post Update-----

I am sorry guys but even dev's code worked only for the first time. I deleted the generated log file and tried creating it again but this time I am getting the same error as "Can not create /temp/testlogfile_error_020609.txt"
Atteched is the screenshot for referance.

-----Post Update-----

Here is the attachement.
How to keep appending a newly created file based on some keywords-errorgif
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Tar file with newly created file

Hello guys thanks for this helpful forum. I'm new to scripting and doing a little script that dump a sql database with some extra files from a VM of the company I'm in, and create a tar archive to be saved on our serve. How can I have the tarball to be created with the files generated in the... (2 Replies)
Discussion started by: dquake
2 Replies

2. AIX

No iscsi available in newly created AIX wpar

AIX 7.1 New to WPAR, hopefully just missing something simple here. Creating the WPAR like this..... (The box where the WPAR is hosted does have an iscsi protocol device) mkwpar -h wpar08 -l -n wpar08 -N interface=en0 address=xxx.xx.xx.xxx netmask=255.255.255.0 -D devname=/dev/iscsi0 -D... (0 Replies)
Discussion started by: TomR
0 Replies

3. Hardware

Formatting a newly created lun

Hi , I have created one new lun in my SAN storage and make it visible to my HP servers , but the fdisk -l output is somehow confusing. Do not know what to do next ---------- fdisk -l /dev/sdo1 Disk /dev/sdo1 (Sun disk label): 64 heads, 32 sectors, 10238 cylinders Units =... (7 Replies)
Discussion started by: mishra.sankar
7 Replies

4. Shell Programming and Scripting

Copy an array to a newly created directory

hello everyone, I am new to perl script and trying to develop a script as follows. I am trying to Create an array for storing all file names. I am trying to copy $libs into "scratch". however i am unable to do so. Please suggest.. #!/usr/bin/perl use File::Copy; #use... (5 Replies)
Discussion started by: Rashid Khan
5 Replies

5. Solaris

Can't see Newly created LUN by SAN admin

hello, i am an oracle DBA and trying to scan a newly created LUN of 200 GB on fiber channel by SAN admin.we have solaris 10 and SANtoolkit is installed.i tried following to get the new LUN at my machine. go /opt/Netapp/Santoolkit/bin and then ./sanlun lun show but i see only the existing... (12 Replies)
Discussion started by: janakors
12 Replies

6. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

7. UNIX for Advanced & Expert Users

default size of a newly created folder

Hi all, In linux how to create a directory with specified size, so that it can be used only up to the mentioned size. Actually my question is, whether we can do directory quota in linux. mounting the directory in a partiton will do that, but do we have any other option... (1 Reply)
Discussion started by: anishkumarv
1 Replies

8. Shell Programming and Scripting

sftp - get newly created files on incremental basis

Hi, We have a sftp server which creates files daily and keeps 6 months of files on the server. We are creating a daily job to get the files and load into database. My problem is "how to get ONLY those files which got created after my last get". Let me provide some more details to it. Below... (15 Replies)
Discussion started by: ravi.videla
15 Replies

9. Shell Programming and Scripting

How to find the newly created directory

Hi, I need to create new directory by increasing the number by 1 of extracted lastly created directory. e.g. Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ become Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ Log\out_log_4\ Can anyone help how to do it in c-shell... (3 Replies)
Discussion started by: Andre_2008
3 Replies

10. UNIX for Advanced & Expert Users

How to FTP all newly created but the current open file?

An application running on HP-UX constantly generates new text log files ( I think using logpipe ). Any new file created requires to be ftp'ed to an offline server, however I want to make sure that the current file being written should not be transferred. For examples consider the following files... (3 Replies)
Discussion started by: indianya
3 Replies
Login or Register to Ask a Question