WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: RegEx to find error but exclude no error  (Read 4689 times)

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
RegEx to find error but exclude no error
« on: July 26, 2017, 12:32:28 AM »
Hi all,

I"m trying to put together a error check in my daily file backup bash script.

Here's my error check section

Code: [Select]
# Check the log file for errors
DateTime=$(date +"%d/%m/%Y %H:%M")
ErrorCheck=$(grep -ciE "fail|can't" $LOGFILE)
if [ $ErrorCheck -gt 0 ]
then
 EmailSubject=$ErrorCheck"(s) Errors Detected in DB Backup $DateTime"
else
 EmailSubject="DSS DB Backup $DateTime"
fi

I've noticed that this will specifiy an error in my email subject if it finds "no error" of course.
Can someone suggest a RegEx that will only find error if its not preceeded by "no " ?

Thanks :)
Live long and prosper.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: RegEx to find error but exclude no error
« Reply #1 on: July 26, 2017, 03:51:34 AM »
grep -vi "no error" $LOGFILE | grep -ciE ...
The only barriers that can stop you are the ones you create yourself.

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: RegEx to find error but exclude no error
« Reply #2 on: July 27, 2017, 12:14:01 AM »
thanks for the input curaga, its still finding my excluded string though.

Here's my update

Code: [Select]
# Check the log file for errors
DateTime=$(date +"%d/%m/%Y %H:%M")
ErrorCheck=$(grep -vi "No errors" $LOGFILE | grep -ciE "error|fail|can't" $LOGFILE)
if [ $ErrorCheck -gt 0 ]
then
 EmailSubject=$ErrorCheck" Errors Detected in DSS DB Backup $DateTime"
else
 EmailSubject="DSS DB Backup $DateTime"
fi

Did I make a simple syntax error maybe ?
Live long and prosper.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: RegEx to find error but exclude no error
« Reply #3 on: July 27, 2017, 03:46:45 AM »
Yes, remove the second $LOGFILE. The second grep should grep the pipe.
The only barriers that can stop you are the ones you create yourself.

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: RegEx to find error but exclude no error
« Reply #4 on: July 27, 2017, 11:09:08 PM »
That nailed it !
Thanks curaga :)
Live long and prosper.