Using sed
:
sed -n ':a;/START/,/END/{/END/!{$!{N;ba;}};/error/p;}' inputfile
The idea is to keep adding the lines in the pattern space between the two specified addresses. If the regex (string error
in this case) is found, then print the block. -n
wouldn't print anything unless explicitly specified.
For your sample input, it'd produce:
START2
error
1
2
END2