grep – show lines until certain pattern
You can use sed
for that:
sed -n '/if/,/endif/p' myfile
-n
don't print anything until we ask for it/if/
find the first line with this,
keep going until.../endif/
this is the last line we wantp
print the matched lines
Traditional grep
is line-oriented. To do multiline matches, you either need to fool it into slurping the whole file by telling it that your input is null terminated e.g.
grep -zPo '(?s)\nif.*\nendif' file
or use a more flexible tool such as pcregrep
pcregrep -M '(?s)\nif.*?\nendif' file
or perl itself
perl -00 -ne 'print if m/^if.*?endif/s' file
Alternatively, for matching structured input in a grep-like way, there's sgrep
sgrep '"if" .. ("endif") containing "SOME CODE"' file
A solution awk
could look like: awk '/if/,/endif/' file
Of course, it is similar to the solution with sed
.