Going back to this discussion: Print all lines between two patterns, exclusive, first instance only (in sed, AWK or Perl)
The proposed solution fails once the ending pattern is a substring of the start pattern.
Example input:
aaa
PATTERNSTART
bbb
ccc
ddd
PATT
eee
Produces output failure:
awk '/PATT/{exit} f; /PATTERNSTART/{f=1}' dat
Gives empty return instead of expected
bbb
ccc
ddd
Corner cases:
Not sure I found all corner cases. Corner cases beyond above might be treated canonically . Thanks.
You must use regex start and end anchors to avoid matching partial patterns:
awk 'f && /^PATT$/{exit} f; /^PATTERNSTART$/{f=1}' dat
bbb
ccc
ddd
or else use string comparison and avoid regex altogether:
awk 'f && $0 == "PATT"{exit} f; $0 == "PATTERNSTART"{f=1}' dat
Make sure you use anchors:
$ awk '/^PATT$/{exit};/^PATTERNSTART$/{f=1;next}; {if (f){print}}' file
bbb
ccc
ddd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With