I have an input like below. I want to find out everything starting with abc
and ending with mno
including lines in between but if abc
appears again before mno
comes, then I want to ignore the first matched abc
. The idea is, I just need a group which starts with abc
and ends with mno
which are nearest to each other.
test.txt
file contains below data:
abc bbb abc yyy mno abc xxx mno
Expected output :
abc yyy mno abc xxx mno
I am using the below grep
liner:
grep -ozP "(?s)(abc).\*?(mno)" test.txt
The result is:
abc bbb abc yyy mno abc xxx mno
The first two lines should not be there in the output. Please advise what I can modify in grep
to get the desired result.