I am trying to match nested text, including the line immediately prior to the nested text with sed or grep.
An example of what I'm working with:
pattern3
abcde
fghij
pattern3
pattern1
abcde
fghij
pattern1
pattern1
klmno
pattern1
pattern3
abcde
pattern1
pqrst
patterh3
fghij
Note that there are always four (4) spaces prefixing the nested text. Also, there may or may not be nested text after a matching pattern.
I'm interested in all pattern1 lines, plus the lines following pattern1 that are proceeded by spaces.
The output I'm looking for is:
pattern1
abcde
fghij
pattern1
pattern1
klmno
pattern1
pattern1
pqrst
I got close with:
sed -n '/^pattern1/,/^pattern1/p' data.txt
But it seems to skip nested text after the right hand side pattern1 match, and move onto the next iteration.
I also tried sed -n '/^\"pattern1\"$/,/^\"pattern1\"$/p' data.txt | sed '1d;$d' with no luck either.