Grep exception but specific exception exception

I am currently matching an Exception from a file and outputting 10 lines before and after use:

grep -C 10 "[. * Exception"

But now I want to exclude some specific exceptions, say AAAException and BBBException , how could I do this? This can be done through

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

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

+5
source share
2 answers

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

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

If your positive match pattern Exception is a word that is not preceded / followed by other alphabets, then you can use a word-boundary.

$ grep -C 10 '\<NullPointerException\>\|\<SessionTimeoutException\>'
0
source

All Articles