I can’t reproduce your problem. Awk* does what I would expect: print each line between the first occurrence of line1
until the first occurrence of line5
:
$ awk '/line1/,/line5/' file
line1
line2
line3
line4
line5
Is it possible that you have a hidden non-printing character somewhere within the string line5
in the fifth line of your file? This would explain why awk
isn’t matching it.
You can double-check by running the sed
equivalent:
$ sed -n '/line1/,/line5/p' file
line1
line2
line3
line4
line5
The -n
instructs sed
to not print every line (its default behaviour) while /line1/,/line5/p
instructs it to print each line from the first match of line1
until the first match of line5
.
If you want to print only the first set of lines starting with a line that matches the pattern line1
and ending with a line that matches line5
, you could use:
sed -n '/line1/,$p;/line5/q' file
* I checked using gawk
, the GNU implementation of awk
(and Kusalananda has confirmed that awk
and mawk
on OpenBSD also do the right thing).
awk
on the example works correctly: https://tio.run/##SyzP/v9fPyczL9VQXwdMm@r//w/mc4FIIzBpDCZNwKQpmDQDk@ZIIkYA – mik Jan 25 '18 at 23:50line1
andline5
. But note that if you have anotherline1
, the block will start again. You'll need to explicitlyexit
or set a flag or something to prevent it – ilkkachu Jan 26 '18 at 11:01