Grep issue

Hi Guys,

I am new to shell scripting. Need help on grep command. I had a file called file.log which contain below statements.

12 Nov 2013 14:12:17,756 INFO security - Userid: raja, Saved File Instance, Name: [SEIBEL], Registry: [RMV]
23 Nov 2013 14:14:11,777 INFO security - Userid: raja, Saved File Entry, Instance Name: [SEIBEL2], Entry Name: [RM_TO_RI857],Registry:
[RMV]
23 Nov 2013 14:14:11,777 INFO security - Userid: raja, Deleted File Entry, Instance Name: [SEIBEL2], Entry Name: [RM_TO_RI857],
Registry: [RMV]

In these i need to grep for 2 patterns "Saved" and "Deleted" and i need to copy the entire line(Not only the word) and append into a new file and need to send email. I tried the below but it doesnt work. Can some one help me please???

#!/bin/ksh
List=~/vinoth/scripts
cd /u/vgomu/scripts
grep -q "Saved" file.log && grep -q "Deleted" file.log > logs
if [ $? -ne 0 ]
then
mailx -s  "***ALERT File ENTRY HAS BEEN MODIFIED******" `cat $List/MailList`
echo 'mail sent' >> logsmail.txt
exit
fi

Your help is much appreciated!!!

/Thanks

Please provide us the error, we can help you

Hi SriniShoo,

There is no error its creating a empty file logs(0 byte file). Instead i need the entire line to be copied to the file and same has to be sent o mails.

/Thanks

You are using the -q option on the grep's , which means "no output" . Another matter is, how you are going to capture the return code of the two commands and the > is overwriting the content every time grep is run, append instead:

So are you trying to do something like this:

if grep -E 'Saved|Deleted' file.log >> logs
then

--
Or do you want to run the second grep only when the first grep finds something and so the lines with "Saved" so not make it to the logs, as is the case now:

if grep -q "Saved" && grep "Deleted" file.log >> logs
then

Hello,

You should put $? -eq 0 in spite of $? -ne 0 , please try that and let us know.

EDIT:
you can use awk for searching patterns.

Thanks,
R. Singh

Hi Scrutinizer/Singh,

Thanks for your help it works!!!

I need one more information. The file called file.log its updated everytime if some one have changed entry in tool. I just want to send an email everytime if file.log is updated with value saved or changed.

Basically my script need to run in background. If some one have updated then i need to recieve mail.

Can anyone help me??

/Thanks

---------- Post updated at 03:03 AM ---------- Previous update was at 01:24 AM ----------

Any Help??Its quite urgent

Hello,

If you want this script to be run every day then cron is the good option. Also for getting a mail when someone have changed file you need to add a extra condition in script by checking the last modification date of file.

This is just an suggestion, so please try and let us know if you have any queries.

Thanks,
R. Singh

Hi Singh,

Thanks for the suggestions!!!

If i use the cron job to run my script everyday. I believe that i will recieve email automatically with existing script. I dont want to make any changes.

Is there any other way that i can design the script to recieve the email as soon as changes happened in the file??

and

In the time of interest how can i schedule the cron job??

I tried to go the path /usr/lib/con. But seems like i dont have permission to login to the path??:mad:

Thanks,
Vinoth

---------- Post updated at 04:21 AM ---------- Previous update was at 03:43 AM ----------

Sorry Singh!! you are correct. I need to modify the existing script to use the cron job:)

Is there any other way to do without

cron

???

Please help me!!!

/Thanks