You can use this sed
:
$ sed -n '/abc 1/,/abc 2/{/abc 1/d; /abc 2/d; p}' file
file content3
file content4
Explanation
/abc 1/,/abc 2/
selectr range of lines from the one containingabc 1
to the one containingabc 2
. It could also be/^abc 1$/
to match the full string.p
prints the lines. So for examplesed -n '/file/p' file
will print all the lines containing the stringfile
.d
deletes the lines.
'/abc 1/,/abc 2/p'
alone would print the abc 1
and abc 2
lines:
$ sed -n '/abc 1/,/abc 2/p' file
abc 1
file content3
file content4
abc 2
So you have to explicitly delete them with {/abc 1/d; /abc 2/d;}
and then print the rest with p
.
With awk
:
$ awk '$0=="abc 2" {f=0} f; $0=="abc 1" {f=1}' file
file content3
file content4
It triggers the flag f
whenever abc 1
is found and untriggers it when abc 2
is found. f
alone will be true and hence print the lines.