A simple sed
expression to extract a block of lines delimited by regular expressions from a text file looks like this:
$ sed -n -e '/start-regex/,/end-regex/ p' input_file
This selects lines from and including the line matching start-regex
up to and including the line matching end-regex
.
The line matching end-regex
may be excluded like this:
$ sed -n -e '/start-regex/,/end-regex/ {/end-regex/d;p}
Is it possible to do this without repeating end-regex
?
If it's possible to omit the last line, then would it follow that it's also possible to omit the first and/or last line without repeating the regexes ?
The reason for this question is to find a more efficient way of solving the problem than repeating expressions which can be complex and hard to read.
This question is about sed
, and a single instance thereof, specifically. There may be ways to do this with pipelines of head
, tail
, awk
, etc, but the question asks if this is possible using sed
only.
There are a number of similar questions but they ask for solutions to specific use-cases rather than dealing with the generic problem at source.
Any solution should work with GNU sed.