I have the following log file. I need to know if there are any fault / suspect condition using a shell script.

I need to find fault and check the previous word, if it is greater than 0 then there is a work for the DBA.

Checking pubs2: Logical pagesize is 4096 bytes
DBCC CHECKSTORAGE for database 'pubs2' sequence 17 completed at Oct 21 2015  3:17PM. 4 faults and 0 suspect conditions were located. 0 checks were aborted. You should investigate the recorded faults, and plan a course of action that will correct them.

I already tried the following commands in Linux/Bash shell and it is working well.

FLTCNT=`cat $MAILLOG | grep -oP '\S+(?=\s+faults and)'`
SPTCNT=`cat $MAILLOG | grep -oP '\S+(?=\s+suspect)'`

if [ $FLTCNT -gt 0 ] || [ $SPTCNT -gt 0 ] ; then
#   echo "Fault / suspect conditions found"
    cat $MAILLOG >> $ERRLOG

But when I execute the same in an AIX server I am getting error

grep: illegal option -- o
grep: illegal option -- P
usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] -e pattern_list...
    [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] [-e pattern_list...]
    -f pattern_file... [file...]
  • Simplest would be to do a check for " 0 faults" (be sure not to include e.g. 10 faults in that) and then proceed if there is no result. Have few tries and then maybe update your question. Apart from that: what have you tried so far? Where are you stuck? And please format your code. – Fiximan Feb 17 '16 at 12:19
  • grep '[1-9]0\?\s\(fault\|check\)' log.file – Costas Feb 17 '16 at 13:00
  • @Costas: Clever, but it doesn't find multiples of 100 (e.g., 4200 faults).  I think grep '[1-9]0*\s…' might work; can you see a problem with it?  P.S. I believe that the OP only wants to look for the word "fault", and not "check". – Scott Feb 17 '16 at 13:31
  • @Swami: Your question shows a two-line file, where one line doesn't contain the word "fault" at all, and the other contains it twice (once preceded by a number, and then again in the phrase "recorded faults").  Is this a realistic, representative example?  If it says "0 faults", will it still say "You should investigate the recorded faults"?  If not, can you just grep for "You should investigate the recorded faults"? And will there be just these two lines, or might there be many (e.g., reporting 0 faults, 0 faults, 42 faults, 0 faults, and 0 faults) and what do you want to do in that case? – Scott Feb 17 '16 at 13:41
  • @Scott, thanks for the reply. I am new to this site and hence the formatting is not correct. The output will have only one faults/suspect/checks. In fact I have to find if there is any value (greater than 0) before any of these three words. I used grep -oP in bash / Linux and it worked, but the same is not working on my AIX server. – Swaminathan Ks Feb 17 '16 at 13:50

Assuming you want to do something if any of the two strings X faults and Y suspect for any positive integer values X and Y not equal to 0 exists in the file $MAILLOG.

if grep -qwE '([1-9]|[0-9]{2,}) (faults|suspect)' "$MAILLOG"; then
    # do something

The pattern ([1-9]|[0-9]{2,}) would match either a single digit greater than zero, or any number with two or more digits.

The pattern (faults|suspect) would match either the string faults or suspect. Would you want to include checks there too, just use (faults|suspect|checks).

The -q option to grep turns off any output non-error output that the utility would otherwise produce (we're only interested in the exit status of grep, i.e. whether it was able to match the pattern or not).

The -w option make grep perform a "word search". In this case, it means that it will find 10 faults rather than the substring 0 faults as the zero in 10 does not start a new "word", but the 1 does. It also means that the string 2 faultsmen (however unlikely that string is) would not trigger a match.

The -E is needed to support extended regular expressions using alternation (|).


You could try using grep:

grep -c "your word to match" /log/file


$ grep -c "upgraded" /var/log/pacman.log 
  • Aside from the fact that it identifies grep as a tool for searching text for patterns, how does this relate to the question? – Scott Feb 17 '16 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.