0

I try to print out the lines between two different patterns(including the patterns) which may accure multiple times in a file. The second pattern may accure besides the startpattern-endpattern-rhytm.

Example:

-File:

XYZ
XYY
BBB
XYX
AAA
AAB
BBB
XYW
AAA
AAC
AAD
BBB

-Output should be like:

AAA
AAB
BBB
AAA
AAC
AAD
BBB

I've already tried things like awk '/AAA/{a=1}/BBB/{print;a=0}a'.

Thank you.

  • The only issue I can see is that you are printing lines that match /BBB/ regardless of the value of a: try changing to awk '/AAA/{a=1} /BBB/ && a {print;a=0} a' – steeldriver Apr 16 at 16:43
0

If it has to be awk just do:

awk -- '/AAA/,/BBB/{print}'

But it seems sed would also be perfectly suited for your case. So, analogously just do:

sed -ne '/AAA/,/BBB/p'

In both commands, use a comma to indicate that you want to consider all the lines starting from (and including) the one matching the first pattern, up to (and including) the line matching the second pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.