1

So, I know that to cut out lines matching a keyword, you can do something like

grep -v "keyword"

And you can get lines before and after with grep using the -A and -B switches... However, when you combine the two...

grep -A 15 -B 1 -v "keyword"

It does NOT cut out all 16 lines of text from the output... It actually doesn't appear to do anything at all, near as I can tell. Is there some other way I can get this functionality, where I can search for a keyword, and then remove defined surrounding content?

Arvandor
  • 181
  • 1
  • 5
  • 15
  • Those answers don't use grep. – Gnubie Apr 15 '15 at 20:36
  • 2
    Don't think of `-v` as removing lines; it just inverts the selection of lines. The `-A` and `-B` options always show context, in this case showing the lines that surround each line that does *not* contain "keyword". – chepner Apr 15 '15 at 20:45

1 Answers1

0

Use grep twice:

grep -v "$(grep -A 15 -B 1 'keyword' FILE)" FILE

to operate on file named FILE.

If it needs to work on stdout, first write it to FILE using

> FILE

then do the above.

Gnubie
  • 2,587
  • 4
  • 25
  • 38