Extracting subset of lines of a file based on regex for first & last line

regular expressiontext processing

I have a large text file and I only want to look at some of the lines. The first line I want matches a regex R, and when the line matches the regex S, I don't care about that line, or any following lines. Lines in the middle will not match R. Is there a way to do this on the command line in a bash command so I can pipe the output somewhere after?

Best Answer

With sed:

sed -n '/R/,$!d; /S/q; p'

Example:

$ seq 20 | sed -n '/6/,$!d; /1/q; p'
6
7
8
9