0

I need to match lines that have MAJOR and CRITICAL strings

that comes after the word ERROR:< any integer number >

please advice how to do that with one awk or sed command?

  more HW_Log.txt



 CHK_HW             ERROR:0  INFO         self_monitor     Verifying HW machine 
 CHK_HW             ERROR:1  MAJOR        self_monitor     Verifying HW machine 
 CHK_HW             ERROR:1  CRITICAL     self_monitor     Verifying HW machine 

3 Answers 3

2

This can be done using grep like:

$ grep "ERROR:[0-9]*  \(CRITICAL\|MAJOR\)" /path/to/file
2

awk command:

awk '/ERROR\:[0-9]+[ \t]+(CRITICAL|MAJOR)/ {print}' path_to_file

sed command:

sed -ne '/ERROR\:[0-9]\+[ \t]\+\(CRITICAL\|MAJOR\)/p' path_to_file

awk description:

 '/ERROR\:[0-9]+[ \t]+(CRITICAL|MAJOR)/ {print}'
  ^                                     ^
  regexp                                command
  • [0-9]+ - one or more digits
  • [ \t]+ - one or more space and tab
  • (CRITICAL|MAJOR) - "CRITICAL" or "MAJOR"
  • print - print line

sed description:

   -n, --quiet, --silent
          suppress automatic printing of pattern space

   -e script, --expression=script
          add the script to the commands to be executed

 '/ERROR\:[0-9]\+[ \t]\+\(CRITICAL\|MAJOR\)/p' 
 ^                                          ^
 regexp                                     command
  • [0-9]\+ - one or more digits
  • [ \t]\+ - one or more space and tab
  • \(CRITICAL\|MAJOR\) - "CRITICAL" or "MAJOR"
  • p - print line
0

Another way to do it in awk:

awk '$2 ~ /^ERROR:[[:digit:]]+/ && $3 ~ /^(MAJOR|CRITICAL)/' HW_Log.txt

This looks for the strings only in the fields indicated so it would ignore them if they appeared elsewhere in the line. It relies on the default behavior of awk to print matched lines (that's why there's no explicit print statement). It depends on no spaces or tabs appearing within the first three fields.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .