Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep ignore multiple lines

Tags:

grep

shell

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.

like image 794
Andrew Avatar asked Sep 29 '09 10:09

Andrew


2 Answers

Try awk:

awk -v nlines=2 '/^Exception/ {for (i=0; i<nlines; i++) {getline}; next} 1'
like image 192
glenn jackman Avatar answered Sep 18 '22 16:09

glenn jackman


sed -n '/^Exception/,+2!p' filename
like image 45
Idelic Avatar answered Sep 19 '22 16:09

Idelic