0

I'd like to do the following:

Given a file with many lines, several of which match a pattern, output the first matching line, the last matching line, and all the lines in between (I want them in their original order, I don't mean I want to rearrange the output). I know I can output surrounding context with grep, but I want one contiguous set of lines as output

Any help appreciated :)

edit: Example:

#PATTERN: 'ZZZZZ'

#INPUT:
foo
foo bar
foo bar ZZZZZ baz
foo bar foo
foo ZZZZZ
bar
ZZZZZ
foo ZZZZZ
foo bar baz
foo

#OUTPUT:
foo bar ZZZZZ baz
foo bar foo
foo ZZZZZ
bar
ZZZZZ
foo ZZZZZ
  • I wouldn’t use grep for that, but rather [`sed` or `awk`](https://stackoverflow.com/q/17988756/2952385) but that answer links to a `grep` Example with pcre as well : https://stackoverflow.com/a/38975890/2952385 – HBruijn Jan 21 '18 at 07:24

1 Answers1

0

Try to use following command if you want to grep lines with "ZZZZZ" and "bar"

grep 'ZZZZZ\|bar' /file/to/path
tsoomo
  • 1
  • 3