Regex grep字符串的部分是否匹配?
我有一个日志文件,其中包含字符串Regex grep字符串的部分是否匹配?,regex,grep,Regex,Grep,我有一个日志文件,其中包含字符串errorCode:null或errorCode:404等。我想查找errorCode不为null的所有事件 我使用: grep -i "errorcode" -A 10 -B 10 app.2020-.* | grep -v -i "errorCode:null"`, grep -v -i 'errorcode:[0-9]^4' -A 10 -B 10 app.2020-.* 但这不是正确的正则表达式匹配。如何
errorCode:null
或errorCode:404
等。我想查找errorCode不为null的所有事件
我使用:
grep -i "errorcode" -A 10 -B 10 app.2020-.* | grep -v -i "errorCode:null"`,
grep -v -i 'errorcode:[0-9]^4' -A 10 -B 10 app.2020-.*
但这不是正确的正则表达式匹配。如何做到这一点?如果您有
gnu grep
,则在regex中使用负前瞻,如下所示,使用-p
选项:
grep -Pi 'errorcode(?!:null)' -A 10 -B 10 app.2020-.*
如果您没有gnu grep
,请尝试此awk
:
awk '/errorcode/ && !/errorcode:null/' app.2020-.*
在
awk
中需要更多的代码位才能匹配grep
的-a10-b10
选项,我不知道您为什么要使用-a10-b10
,我刚刚使用了以下命令,一切正常:
grep -i "ErrorCode" test.txt | grep -v -i "Errorcode:null"
这将更改文件内容:
Errorcode:null
Errorcode:405
Errorcode:504
Nonsens
这是命令的输出:
Errorcode:405
Errorcode:504
我是否可以将多个参数传递给not标志,如not null、not 111、not 1201?。我有gnu grep它工作正常谢谢errorcode(?:null |?!:111)这样好吗?是的,使用
grep-Pi'errorcode(?)(:null | 111 | 1201)\b)肯定是可能的,通常这会给我匹配的errorcode:1111201,如果你在errorcode
之后有,grep-Pi'errorcode(?:(null | 111 | 1201)\b)“
比赛前后总共有10行,谢谢apprach@Rahul:不客气,但是当你添加前面和后面的10行时,你不怕添加你不想要的行吗?不,我实际上想让这些行检查出了什么问题。