我试着逐行检查日志文件里的档案。对于每一行,检查具体字段是否为空或字段的值是否错误。并打印包含错误消息的行
#!/bin/bash
LOG=/log/output.log
x=false
while read -r line; do
if
(echo $line | awk -F'|' '{if ($8=="") print "Application is empty"}') ||
(echo $line | awk -F'|' '{if ($9=="") print "Http request method is empty"}') ||
(echo $line | awk -F'|' '{if ($7=="") print "Operation is empty"}')
(echo $line | awk -F'|' '{if ($13 !~ /0|1|2/) print "result(0,1,2) has a wrong value"}')
then
echo $line
fi
done < "$LOG"实际结果:
9f2b|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:40.095Z|1|0|14|19|||XXXXX|897|5b8689707|
应用程序为空
9f2b|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:40.095Z|18|0|XXXXX|1234|5b868007|
应用程序为空
42e2|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.532Z|22|0|XXXXX|235|3b6959ae|
应用程序为空
83ac|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.758Z|8|0|XXXXX|789|5945548f|
预期结果:
应用程序为空
操作为空
Http请求方法为空
83ac|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.758Z|8|0|XXXXX|789|5945548f|
发布于 2019-05-14 19:04:46
awk读取文件的每一行,所以不需要使用echos的while循环。只需这样做:
awk -F\| ' {b=1}
$8=="" { print "Application is empty"; b=0 }
$9=="" { print "Http request method is empty"; b=0 }
$7=="" { print "Operation is empty"; b=0 }
$13 !~ /0|1|2/ {print "result(0,1,2) has a wrong value"; b=0 }
b
' /log/output.log这种方法的主要问题是,使用if计算的命令总是成功的,所以总是打印一行。要使awk失败,必须将exit语句添加到每个awk语句中。(如echo $line | awk -F'|' '$8=="" {print "Application is empty"; exit 1 }'
如果一行不符合多个条件,这将打印多条错误消息,如果添加exit语句并缩短||运算符,则不会输出多条错误消息。
如果只想打印一条错误消息,可以执行以下操作:
awk -F\| '
$8=="" { print "Application is empty"; next }
$9=="" { print "Http request method is empty"; next }
$7=="" { print "Operation is empty"; next }
$13 !~ /0|1|2/ {print "result(0,1,2) has a wrong value"; next }
' /log/output.log或者,如果您想打印多条错误消息,但全部在一行上,您可以这样做:
awk -F\| ' {s=""}
$8=="" { printf "Application is empty"; s="; " }
$9=="" { printf "%sHttp request method is empty", s; s="; " }
$7=="" { printf "%sOperation is empty", s; s="; " }
$13 !~ /0|1|2/ {printf "%sresult(0,1,2) has a wrong value", s; s="; " }
s { print ""}
!s
' /log/output.loghttps://stackoverflow.com/questions/56128912
复制相似问题