I have a file like the following and I would like to print the lines between two given patterns PAT1
and PAT2
.
For completeness, here is a Perl solution:
perl -ne '/PAT1/../PAT2/ and print' FILE
or:
perl -ne 'print if /PAT1/../PAT2/' FILE
perl -ne '/PAT1/../PAT2/ and !/PAT1/ and !/PAT2/ and print' FILE
or:
perl -ne 'if (/PAT1/../PAT2/) {print unless /PAT1/ or /PAT2/}' FILE
perl -ne '/PAT1/../PAT2/ and !/PAT1/ and print' FILE
perl -ne '/PAT1/../PAT2/ and !/PAT2/ and print' FILE
See also:
perldoc perlop
for more on the /PAT1/../PAT2/
grammar:Range operator
...In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors.
For the -n
option, see perldoc perlrun
, which makes Perl behave like sed -n
.
Perl Cookbook, 6.8 for a detailed discussion of extracting a range of lines.