我需要匹配具有MAJOR和CRITICALstring的行
在ERROR:<any integer number>之后
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
这可以使用grep来完成,例如:
$ grep "ERROR:[0-9]* \(CRITICAL\|MAJOR\)" /path/to/file
awk命令:
awk '/ERROR\:[0-9]+[ \t]+(CRITICAL|MAJOR)/ {print}' path_to_file
sed命令:
sed -ne '/ERROR\:[0-9]\+[ \t]\+\(CRITICAL\|MAJOR\)/p' path_to_file
awk描述:
'/ERROR\:[0-9]+[ \t]+(CRITICAL|MAJOR)/ {print}' ^ ^ regexp command
sed说明:
-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
在awk中做的另一种方法是:
awk '$2 ~ /^ERROR:[[:digit:]]+/ && $3 ~ /^(MAJOR|CRITICAL)/' HW_Log.txt
这只在所指示的字段中查找string,所以如果它们出现在行中的其他位置,它将忽略它们。 它依赖于awk的默认行为来打印匹配的行(这就是为什么没有明确的print语句)。 这取决于前三个字段中没有空格或制表符。