How to print all the lines not containing a string along with adjacent lines

text processing

I'm trying to wrap my mind around this task. I want from the following output, to exclude all the lines that contain Auto-installed: 1 and the 2 lines before. I was naively thinking that grep -v -B2 string will solve the problem… needless to say, only makes it worse.

This is the content:

Package: libopencore-amrwb0
Architecture: amd64
Auto-Installed: 1

Package: transfig
Architecture: amd64
Auto-Installed: 0

Package: xfig-libs
Architecture: amd64
Auto-Installed: 1

Package: xaw3dg
Architecture: amd64
Auto-Installed: 0

And this is how the results look:

Package: transfig
Architecture: amd64
Auto-Installed: 0

Package: xaw3dg
Architecture: amd64
Auto-Installed: 0

(note, empty lines are not showing, if it doesn't show empty lines is a plus but is not required, also results should include the Packages which Auto-Installed values is 0)

I know I can match line breaks but either end in not printing anything or printing everything but the lines breaks in between.

Any solution is acceptable, it doesn't have to be grep (it even can be emacs).

Best Answer

I'm not sure how structured your data is but if it is just as you show, how about the following:

grep -B2 'Auto-Installed: [^1]'

That assumes that every stanza includes an Auto-Installed line, which might not be correct.

Here's an awk program which I think does exactly as you asked.

awk 'BEGIN{deleted=3}
     !deleted{printf "%s",l[NR%3]}
     deleted {--deleted}
     {l[NR%3]=/./?$0"\n":$0}
     /Auto-Installed: 1/{deleted=3}
     END{for(i=NR+deleted;i<NR+3;++i)printf "%s",l[i%3]}'
Related Question