Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy match in unix bash shell script

I am new to shell scripting and unix commands and am trying to find a good way to do a lazy match between patterns in a file and haven't been able to get the output I want to see. The text looks like the following example (Edit: Revised text string

"hello
how
are
you
right
now
and
hey how
you
doing
thanks
see
you
and
ask
me if I
am
doing
okay"

Desired output:

hey how
you
doing

I have tried to use multiple answers I found here for similar issues involving sed, grep or awk and I keep seeing this output instead:

how
are
you
right
now
and
hey how
you
doing
thanks
see
you
and
ask
me if I
am
doing

I just want to perform a lazy match between the word "how" and "doing" and print the lines in between (inclusive) (and preferably how to use the solution to assign the returned text to a variable in a shell script)

Thanks in advance

Edit

like image 219
wicke_s Avatar asked Nov 20 '25 21:11

wicke_s


1 Answers

I just want to perform a lazy match between the word "how" and "doing" and print the lines in between

You may use this awk:

awk 'p{s = s ORS $0} /how$/{p=1; s=$0} p && /doing$/{print s; p=0}' file

hey how
you
doing
like image 57
anubhava Avatar answered Nov 23 '25 13:11

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!