Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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
add comment

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
share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.