Calisthenics
Calisthenics

Reputation: 21

Grep everything after pattern whose # of lines are inconsistent

I'm trying to find a way to grep out a specific text out of a bunch of similar files. Example:

-
Blah1: stuff
Blah2: stuff
Blah3:
 - bingo
 - this
 - is
 - what im looking for
Blah4: stuff
-
Blah1: stuff
Blah2: stuff
Blah3:
 - bingo this is
 - what im looking for
Blah4: stuff

However the "bingo this is what I'm looking for" isn't always 4 lines and is sometimes 2 or 15 lines following same pattern but always starts with a new line and proceeded by - and Blah3 is always Blah3

I need a way of getting an output that shows just

Blah3:
- Bingo
- this
- is
- what im looking for
Blah3:
- bingo this is
- what im looking for

Tried using -A on grep but the number of lines that come sfter Blah3: are inconsistent and tried using quite a bit of regex with grep but still can't get the output I'm lookikg for

Upvotes: 0

Views: 69

Answers (1)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed -n '/^\S\+:/h;G;/^Blah3:/MP;d' file

Turn off implicit printing by using the -n option.

Copy any key to the hold space.

Append the hold space to each line.

If the required key is present, print the first line in the pattern space.

N.B. The M flag on a regexp allows the ^ to match at the beginning of a line.

Alternative:

sed -n '/^Blah3:/{:a;p;n;/^ -/ba}' file

Upvotes: 1

Related Questions