shell 脚本 +匹配日志文件中的错误词
请建议如何匹配仅出现在“]”字符之后的错误字符串 通过 awk 或 sed
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
我的目标是统计日志文件中仅出现在“]”字符之后的所有 ERROR 单词备注
- “]”和 ERROR 之间必须是一个或多个空格
please advice how to match the ERROR strings that comes only after "]" char
by awk or sed
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
My target is to count all ERROR words that appears only after "]” character in the log file
remark - between “]” and ERROR must be one space or more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
滿滿的愛2025-01-08 00:09:35
AWK:
awk -F"] " '/ERROR/{print $2}' inputfile
测试:
[jaypal:~] echo "[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found " | awk -F"] " '/ERROR/{print $2}'
ERROR file /etc/ghy.txt not found
Perl:
perl -pe 's/.*(?<=] )(.*)/$1/' inputfile
测试:
echo "[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found " | perl -pe 's/.*(?<=] )(.*)/$1/'
ERROR file /etc/ghy.txt not found
计数编号。出现次数:
[jaypal:~/Temp] cat file
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
[jaypal:~/Temp] awk -F"] " '/ERROR/{a[NR]=$2}END{print "count is " length(a)}' file
count is 9
愁以何悠2025-01-08 00:09:35
这是一个仅 shell 的代码片段,它应该比使用任何外部程序(用于小文件读取)更快,因为它只使用 shell 内置程序。可以对其进行修改,以在守护进程模式下运行时逐个处理错误(通过将日志文件尾部到 fifo,而不是直接读取它并修改 case 条件),
这不是 echo 的预期用途,但它确实将空格/制表符减少到 1 个空格
FILE="logfile"
ERRORS=0
while read LINE || [ "$LINE" ]; do
case "`echo $LINE`" in
*\]" "ERROR*)ERRORS=$(($ERRORS+1));;
esac
done < "${FILE}"
echo $ERRORS
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
,这样你就不需要像 awk、sed 甚至 perl 这样的核头了。 grep 为你做这样的事情:
小测试:
then you don't need those nuclear heads like awk, sed even perl. grep does it for you like this:
small test: