Awk/sed command to extract the string between 2 patterns but having some particular value

Hi -

i have one file with content as below.

***** BEGIN 123 *****
BASH is awesome
***** END *****
***** BEGIN 365 *****
KSH is awesome
***** END *****
***** BEGIN 157 *****
KSH is awesome
***** END *****
***** BEGIN 7123 *****
C is awesome
***** END *****

I am trying to find all the lines in between BEGIN and END but having KSH is awesome

I was using awk '/BEGIN/,/END/' MyFileName
but it is giving me all the content. How do i extract only below 2, kind of having clause between the 2 strings.

***** BEGIN 365 *****
KSH is awesome
***** END *****
***** BEGIN 157 *****
KSH is awesome
***** END *****

Pls suggest a way around

Untested:

awk '{store=(store ORS $0)} /BEGIN/ {store=$0} /END/ && store ~ /KSH is awesome/ {print store}' MyFileName

One store variable -> can be done with sed and its hold space.

sed -n 'H; /BEGIN/h; /END/!d; x; /KSH is awesome/p' MyFileName
3 Likes

Hi,

For something like this, just use.

awk '/KSH is awesome/ { print $0 }' MyFilename

Didn't read the post properly, however I see it's been answered.

Regards

Gull04

this will only show the lines with KSH is awesome .. it will not showing the lines between BEGIN/END

--- Post updated at 08:14 AM ---

MadeInGermany
your command mention below is working fine (though difficult to remember for future uses :slight_smile: ]

awk '{store=(store ORS $0)} /BEGIN/ {store=$0} /END/ && store ~ /KSH is awesome/ {print store}' MyFileName

thanks a lot, appreciate your help on this.

Hi,

Yes I was aware of the mistake, however by the time that I went in to rectify it -Madein Germany had posted two solutions, which is why I updated the post the way I did.

Regards

Gull04

1 Like
Moderator comments were removed during original forum migration.