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.

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:

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:

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.

when you are using sort use -u option instead of uniq

newlogname=/parentfolder/Archive/testlogfile_error_`date '+%d%m%y'`.txt
touch $newlogname
egrep "Error|error|Failed|failed" /parentfolder/someLogFile.log >> $newlogname

-Devaraj Takhellambam

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

Hi devtakh, Thanks a lot your code worked just fine.:slight_smile:
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.