Issue
How do I use sed to select every line matching a pattern + the next line? For instance, I'd like to select all lines with tag="foo" plus the next line. As an alternative, I'd also like to be able to select lines with tag="foo" OR group="bar" plus the next line.
Solution
This might work for you (GNU sed):
sed -En '/tag="foo"|group="bar"/,+1p' file
Turn on extended regexp -E
and off implicit printing -n
.
Match the alternation of tag="foo"
or group="bar"
and print the range +1
line(s).
Alternative:
sed '/tag="foo"\|group="bar"/!d;n' file
To always print 2 lines, use:
sed -n '/tag="foo"\|group="bar"/{N;p}' file
Answered By - potong Answer Checked By - Robin (WPSolving Admin)