I have a text file file.txt
with the content:
lord
blue
bird
The sky
reptile
lorwood
wood
I’m trying to print the lines between "lord" and "reptile" using command sed
and Basic Regular Expression as:
$ sed -n '/^lor/,/^rep/p' file.txt
However, it prints the whole file, like a cat
. Why?
The version of sed
I’m using is:
$ sed --version
sed (GNU sed) 4.8
Packaged by Debian
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
Paolo Bonzini, Jim Meyering, and Assaf Gordon.
This sed program was built with SELinux support.
SELinux is disabled on this system.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
2
Answers
You have a second
lor
line immediately after therep
line so sed is printing fromlord
toreptile
and then fromlorwood
til the end of the file as 2 separate blocks which together encompass all of the lines from your input.You can see the 2 separate blocks if you use awk and a flag instead of sed and a range expression:
or, if you prefer:
FWIW, I wouldn’t use a range expression
/start/,/end/
(and so wouldn’t usesed
) for this as it’s too hard to control the output, I’d useawk
and a flag instead, see Is a /start/,/end/ range expression ever useful in awk?.This might work for you (GNU sed):
This is essentially a filter operation so use the
-n
command line option to suppress the implicit printing of each line.Match
lor
and using a loop, gather up future lines until the collection containsrep
, then print the collection and repeat.N.B. Only a valid collection will be printed out by the
p
command, once the loop is ended.