Issue
Say I've got input that looks like this:
something
START MARKER
foo
I
just
want
this
foo
END MARKER
something else
foo
I don't want this
foo
Can I use sed
to get the text that is between both START MARKER
and END MARKER
and between the foo
s, which appear as nested markers?
Currently I'm able to do this with two passes:
sed -n '/START MARKER/,/END MARKER/ { // !p; }' < input \
| sed -n '/foo/ !p;'
However, I feel like this isn't as optimal as it could be...
Solution
OP's attempt can be simplified to delete the lines containing foo
along with the start and end markers:
sed -n '/START MARKER/,/END MARKER/ {//d; /foo/d; p}'
This works with GNU sed
, but other implementations might differ w.r.t. the behavior of the empty pattern //
.
Answered By - Sundeep Answer Checked By - David Marino (WPSolving Volunteer)