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
1
0

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

grep 'ZZZZZ\|bar' /file/to/path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy