pmv
pmv

Reputation: 357

regex to identify failures in log

I have a log file. I want to scan the occurrences of the string

"RC = x" If there is >=1 one occurrence where RC = 0 is not TRUE then I want to flag it as failure

Can I do this as shell function?

Upvotes: 1

Views: 42

Answers (1)

Greg Tarsa
Greg Tarsa

Reputation: 1642

You can use grep for this:

grep -q "RC = [^0]" logfile && {
  echo "Logfile is a failure"
  other commands
  exit
}

The -q puts grep into silence mode: no output, the [^0] matches any non-zero value for RC. Grep will return a failure status if no matches are found and a success status if any are. The && is a conditional "and" so if there are any non-zero values for RC, then the grep will succeed and the test is a failure.

Alternately, you can put the grep into an if statement. In this case:

if grep -s "RC = [^0]" logfile
then
  echo "Logfile is a failure"
else
  echo "Logfile is a success"
fi

See man grep for more grep options.

Upvotes: 1

Related Questions