Output Lines Between Two Matching Lines
This page answers questions like these:
- How to output only the lines between two matching lines?
- How to output only the lines between two matching patterns?
- How to output only the lines between two matching strings?
- How to extract the lines between two matching lines?
- How to extract the lines between two matching patterns?
- How to extract the lines between two matching strings?
Related Links:
Delete Lines Between Two Matching Lines
Output Lines of a File in Reverse Order
Count Occurrences of a Hexadecimal Sequence in a File
Count Occurrences of a String in a File
Find Positions of a Hexadecimal Sequence in a File
Join Lines of Text File Together
Output the Lines Between Two Matching Lines (Inclusive)
sed -n '/PATTERN1/,/PATTERN2/p' FILE ...
- For each file in FILE ... start at the first line of the file and do the following:
- Find the next line matching PATTERN1. Output it to stdout.
- Find the next line matching PATTERN2. Output all lines along the way (even if PATTERN2 cannot be found). Output the line matching PATTERN2 (if any).
- Go back to step 1.
- Pros: Handles multiple files.
- Cons: Assumes matching pairs: If a file ever has a line matching PATTERN1 with no subsequent line matching PATTERN2, all lines after and including the one matching PATTERN1 are still output. Assumes no nesting of pairs.
Output the Lines Between Two Matching Lines (Exclusive)
sed -n '/PATTERN1/,/PATTERN2/{
/PATTERN1/b
/PATTERN2/b
p
}' FILE ...
- For each file in FILE ... start at the first line of the file and do the following:
- Find the next line matching PATTERN1. Do not output this line.
- Find the next line matching PATTERN2. Output all lines along the way (even if PATTERN2 cannot be found). Don’t output the line matching PATTERN2.
- Go back to step 1.
- Pros: Handles multiple files.
- Cons: Assumes matching pairs: If a file ever has a line matching PATTERN1 with no subsequent line matching PATTERN2, all lines after the one matching PATTERN1 are still output. Assumes no nesting of pairs.
- Caveats: On some systems, it may be necessary to append a backslash (\) to the end of each of the first 4 lines in the command above. The sed commands above can also be placed on a single line by terminating each command with a semi-colon (;).
Related Links:
Delete Lines Between Two Matching Lines
Output Lines of a File in Reverse Order
Count Occurrences of a Hexadecimal Sequence in a File
Count Occurrences of a String in a File
Find Positions of a Hexadecimal Sequence in a File
Join Lines of Text File Together
Home > Linux / Unix > Output Lines Between Two Matching Lines
Tags: show lines between two matching patterns, show lines between two matching lines, linux, unix, solaris, bsd, aix
Copyright © HelpDoco.com
file-output-only-lines-between-two-matching-lines.txt
Linux-Unix/output-only-lines-between-two-matching-lines.htm
2