tags:

views:

360

answers:

4

Is there a way with Grep to use the -v switch to ignore a line and the next number of lines after it. Its basically to filter exceptions from a log file i.e.

Valid log entry 1
Exception exceptionname
    at a.b.c
    at d.e.f
    ...
Valid log entry 2

grep it to produce :

Valid log entry 1
Valid log entry 2

I have tried grep -v Exception -A 2

but this doesn't work.

Any ideas would be appreciated.

+1  A: 

In your case, you might tell grep to also ignore any lines that contain leading whitespace:

grep -v 'Exception|^\s'
Adam Batkin
+2  A: 

Try awk:

awk -v nlines=2 '/^Exception/ {for (i=0; i<nlines; i++) {getline}; next} 1'
glenn jackman
A: 

Can do something like this in a Perl one-liner:

perl -pe 'if (/Exception/) {$_=<> for 1..2; $_="--\n"}' filename

This will replace any line that matches "Exception" and the following two lines with a line with two dashes.

mobrule
+1  A: 
sed -n '/^Exception/,+2!p' filename
Idelic