So my dear SOers, Let me be direct to the point:
specification: filter a text file using pairs of patterns.
Example: if we have a file:
line 1 blabla
line 2 more blabla
line 3 **PAT1a** blabla
line 4 blabla
line 5 **PAT1b** blabla
line 6 blabla
line 7 **PAT2a** blabla
line 8 blabla
line 9 **PAT2b** blabla
line 10 **PAT3a** blabla
line 11 blabla
line 12 **PAT3b** blabla
more and more blabla
should give:
line 3 **PAT1a** blabla
line 4 blabla
line 5 **PAT1b** blabla
line 7 **PAT2a** blabla
line 8 blabla
line 9 **PAT2b** blabla
line 10 **PAT3a** blabla
line 11 blabla
line 12 **PAT3b** blabla
I know how to filer only one part of it using ‘sed’:
sed -n -e ‘/PAT1a/,/PAT1b/{p}’
But how to filter all the snippets, do i need to write those pairs of patterns in a configuration file, read a pair from it, use the sed cmd above, go to next pair…?
Note: Suppose PAT1, PAT2 and PAT3, etc share no common prefix(like ‘PAT’ in this case)
One thing more: how to make a newline in quota text in this post without leaving a whole blank line?
I assumed the pattern pairs are given as a separate file. Then, when they appear in order in the input, you could use this
awkscript:And a more complicated version when the patterns can appear out of order: