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

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

问题描述

我目前正在匹配文件中的Exception,并在使用前后输出10行:


grep -C 10 [。* Exception


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


grep -vAAAException| grep -C 10[。* Exception


但是如果在文件中,我有 AAAException 在其他异常的10行内,那行不会包含在输出中,这不是我想要的。我如何不能匹配 AAAException ,但是如果它发生在其他异常的10行内,它仍然会包含在输出中?

$ b $如果你有 grep -P ,你可以指定一个消极的后向断言。

b
$ b

  grep -C 10 -P'\ [。*(?<!AAA | BBB)异常'


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

grep -C 10 "[.*Exception"

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"

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?

解决方案

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

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

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

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