Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 
share|improve this question
up vote 2 down vote accepted

This can be done using grep like:

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

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
  • 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
  • p - print line
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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