Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep Exception but exclude specific Exception

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?

like image 526
Kathie Avatar asked May 28 '12 06:05

Kathie


1 Answers

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

grep -C 10 -P '\[.*(?<!AAA|BBB)Exception' 
like image 57
tripleee Avatar answered Oct 09 '22 13:10

tripleee