InterviewSolution
| 1. |
How Do I Locate A Paragraph Of Text If The Paragraph Contains A Certain Regular Expression? |
|
Answer» Assume that PARAGRAPHS are SEPARATED by blank lines. For regexes that are SINGLE terms, use one of the following scripts: sed -e '/./{H;$!d;}' -e 'x;/REGEX/!d' # most seds To print paragraphs only if they contain 3 specific regular EXPRESSIONS (RE1, RE2, and RE3), in any order in the paragraph: sed -e '/./{H;$!d;}' -e 'x;/RE1/!d;/RE2/!d;/RE3/!d' With this solution and the preceding one, if the paragraphs are excessively long (more than 4k in length), you may overflow sed's internal buffers. Assume that paragraphs are separated by blank lines. For regexes that are single terms, use one of the following scripts: sed -e '/./{H;$!d;}' -e 'x;/regex/!d' # most seds To print paragraphs only if they contain 3 specific regular expressions (RE1, RE2, and RE3), in any order in the paragraph: sed -e '/./{H;$!d;}' -e 'x;/RE1/!d;/RE2/!d;/RE3/!d' With this solution and the preceding one, if the paragraphs are excessively long (more than 4k in length), you may overflow sed's internal buffers. |
|