Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems grep-ing for a string in a file - Need an if loop 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I am executing a script which runs every 30 minutes and greps for a string (FA) in a file and e-mails myself if the string is found, however each time this script runs if the script is found I receive the same information until the next day. I.E. At 08:00 I receive a notification that 3 jobs have failed. Those jobs will remain in the file that I am grep-ing in as FA (failed) because I am appending to the file, however I do not want to receive a notification each time the script runs for a FAILURE which happened hours ago; I only want to be notified of the failure one time. How should I solve this problem? Below is the portion of the script that I am having difficutlies with. Any information will be greatly appreciated.

FAIL=`grep -c FA casfinalreport.$JGDATE4`

if [ $FAIL -ne 0 ]
then

FAILURES=`grep FA casfinalreport.$JGDATE4 | awk -F, '{print $1}'`
for job in `echo $FAILURES`; do
SUBJECT="$SUBJECT $job,"
done

grep FA casfinalreport.$JGDATE4 | awk '{print $1;}' | mailx -s "The Following Job(s) Have Failed: $SUBJECT" test@test.com
fi

Thanks,

John
 
here's one way/hint to do it.
assuming your log file gets APPENDED and you ONLY want to report/mail NEW matched/grep-ed lines...

use 'grep -n' - this will give matchig lines with their corresponding line numbers. Again, assuming the new failures get appended to a file, save LAST matched line number in a "/tmp/lastLine.txt" [or whatever] and reuse that number for every invokation of your script. The matching lines that have their line numbers BELOW the previously saved [in the /tmp/lastLine.txt] will be discarded - the new ones will be acted/mailed on.

The rest is a matter of simple implementation excersise [wink] vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Here's what I do to check my error logs, you should be able to bend it to suit your needs

#!/bin/ksh

TOTALERRS=`errpt|grep -v &quot;IDENTIFIER&quot; | wc -l`
OLDERRS=`cat /usr/local/bin/errpt.count`
((NEWERRS=TOTALERRS-OLDERRS))

if [ ${NEWERRS} -gt 0 ]
then
errpt | grep -v &quot;IDENTIFIER&quot;|head -${NEWERRS} | cut -c 42- |
while read ERRMSG
do
echo &quot;Error:${ERRMSG}&quot; | /usr/bin/mailx -c &quot;mike@test.com&quot;
&quot;adrian@test.com&quot;
done
fi

echo ${TOTALERRS} > /usr/local/bin/errpt.count --
| Mike Nixon
| Unix Admin
| ----------------------------
 
Hi,
A simple way to do these things is to make a copy of the file each time an error is found, then use diff to see if the current file is any different than the last file with an error. Something like this (first create a casfinalreport.last as an empty file):

diff casfinalreport.$JGDATE4 casfinalreport.last > casdiff
FAIL=`grep -c FA casdiff`

if [ $FAIL -ne 0 ]
then

FAILURES=`grep FA casdiff | awk -F, '{print $1}'`
for job in `echo $FAILURES`; do
SUBJECT=&quot;$SUBJECT $job,&quot;
done

grep FA casdiff | awk '{print $1;}' | mailx -s &quot;The Following Job(s) Have Failed: $SUBJECT&quot; test@test.com
cp casfinalreport.$JGDATE4 casfinalreport.last
fi
 
Excellent idea mrregan!! That works great!! Thank you all for your help. I really appreciate it!!

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top