I've seen a few examples of this, but I can't seem to get it to work in my particular situation. Lets say there is the below file.
foo
line 1
line 2
line 3
bar
junk
junk
foo
line 1
line 2
baz
line 4
bar
I'm trying to catch everything between 'foo' and 'bar' as long as it contains 'baz' with a one liner.
Everything I've found so far is great for finding everything between foo and bar, but nothing seems to be good for only finding it if it contains baz.
Edit: The below works for me:
sed -n '/foo/{:a;N;/bar/!ba; /baz/p}' input.txt
foo
andbar
be also included into result? – RomanPerekhrest May 25 '17 at 20:49pcregrep -Mo '(?s)foo((?!bar).)*?baz((?!bar).)*?bar'
would do it in a not very efficient way. – Stéphane Chazelas May 25 '17 at 20:55