[sed] Print lines between start and end patterns

Created on November 30, 2026

Use sed -n '/start-pattern/,/end-pattern/p' to print a range of lines matched by two regular expressions. The output includes both the start and end lines.

Suppose report.log contains these lines.

connecting
BEGIN
received 12 records
validated 12 records
END
disconnected

The command below prints the range from the line containing only BEGIN through the line containing only END.

sed -n '/^BEGIN$/,/^END$/p' report.log

This produces the following output.

BEGIN
received 12 records
validated 12 records
END

-n suppresses automatic printing, and p prints lines within the selected range. The ^ and $ anchors require each marker to match the whole line.

If another start line appears after the end line, sed prints that range too. If the start line is followed by no end line, sed prints through the end of the input.

References

Shell command sed