grep 异常但排除特定异常 [英] grep Exception but exclude specific Exception

查看:22
本文介绍了grep 异常但排除特定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从文件中匹配异常",并在使用前后输出 10 行:

I am currently matching for "Exception" from a file, and output 10 lines before and after using:

grep -C 10 "[.*Exception"

grep -C 10 "[.*Exception"

但现在我想排除某些特定的异常,比如 AAAExceptionBBBException,我该怎么做它?可以通过

But now I wish to exclude certain specific Exceptions, say AAAException and BBBException, how could I do it? It can be done via

grep -v "AAAException" |grep -C 10 "[.*Exception"

grep -v "AAAException" | grep -C 10 "[.*Exception"

但是如果在文件中,我有 AAAException 来自其他一些异常的 10 行内,​​该行将不会包含在输出中,这不是我想要的.我怎么能不匹配 AAAException,但如果它发生在距其他一些异常的 10 行以内,它仍将包含在输出中?

But if within the file, I have AAAException within 10 lines from some other Exception, that line would not be included in the output, which is not what I want. How can I not match for AAAException, but if it occurred within 10 lines from some other Exception, it will still be included in the output?

推荐答案

如果你有 grep -P 你可以指定一个否定的lookbehind 断言.

If you have grep -P you can specify a negative lookbehind assertion.

grep -C 10 -P '[.*(?<!AAA|BBB)Exception' 

这篇关于grep 异常但排除特定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆