print lines between start and end using sed

sed -n -e '/^BEGIN$/,/^END$/{/^BEGIN$/d;/^END$/d;p;}' input

With GNU sed 3.95 or above, you can do:

sed '/^BEGIN$/,/^END$/!d;//d'

With other seds, you may have to write it:

sed '/^BEGIN$/,/^END$/!d;//d;/^BEGIN$/d'

Or even

sed '/^BEGIN$/,/^END$/!d;/^END$/d;/^BEGIN$/d'

like with busybox sed.

See also the sed FAQ


If you don't want to repeat the delimiters you could use Perl:

perl -ne '/BEGIN/ && do {$a=1; next}; $a=0 if /END/; print if $a' input

You could also modify HaukeLaging's answer to use variables:

b="BEGIN"; e="END"; sed -n -e "/^$b$/,/^$e$/{/^$b$/d;/^$e$/d;p}" input

Tags:

Sed