My file, dummy.txt, reads as follows.
Quote:
AAA
blah
x
y
z
ZZZ
BBB
blah
x
y
z
ZZZ
|
It is two 'blocks' of text. Each block is enclosed by two regular expressions. The end of each block is always the same. In this case ZZZ, but the start of each block can vary.
Using sed to print lines between two regular expressions. Getting the lines between AAA and ZZZ, inclusive, works with the follow sed command.
Quote:
> cat dummy.txt | sed -n '/AAA/,$p; /ZZZ/q'
AAA
blah
x
y
z
ZZZ
|
Getting the lines between BBB and the next ZZZ does not work using exact syntax.
Quote:
> cat dummy.txt | sed -n '/BBB/,$p; /ZZZ/q'
|
Nothing is output with the second command but I was expecting this.
Apparently my sed command doesn't like it if the second expression is found before the first expression. By changing the 'q' to a 'd' in the sed command I am able to extract the second block.
Quote:
> cat dummy.txt | sed -n '/BBB/,$p; /ZZZ/d'
BBB
blah
x
y
z
ZZZ
|
Unfortunately this syntax breaks when extracting the first block.
Quote:
> cat dummy.txt | sed -n '/AAA/,$p; /ZZZ/d'
AAA
blah
x
y
z
ZZZ
BBB
blah
x
y
z
ZZZ
|
I suppose the 'd' is telling sed to find the last occurrence of the regular expression. Since this is part of a loop the same syntax is needed for all blocks.
My real file has more blocks, 24, and the expressions are not as simple but the concept is the same.