How to search multiple Pattern in grep while ignoring the first pattern if appearing consecutively

Neeraj Mishra

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.

jai_s
grep -ozP  "(?s)(abc)[^(abc)]*(mno)" 1
abc
yyy
mno
abc
xxx
mno

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

grep,how to search for exact pattern?

How to search for a pattern using grep?

How to break a grep regex search pattern

How to match for an empty string in a grep pattern search?

How to display grep results with the search pattern omitted?

Find for the maximum occurrences of a particular pattern appearing consecutively within a substring [Python]

grep pattern exacly matching from file and search only in first column

perl grep multiple pattern

how to exclude multiple pattern using grep

How to grep a multiple pattern per line

how to get a regex pattern to match consecutively?

grep search pattern in multiple files and output to different files

Search with grep for space and number pattern

grep search for the exact pattern in R

How to search for multiple words of a specific pattern and separator?

How to grep for this pattern in Unix

How to use grep to search for a pattern which starts with a hyphen (-)?

How to search for a line before a pattern using sed or grep

Grep multiple pattern negative match

Grep pattern with Multiple lines with AND operation

grep multiple strings for a match pattern

grep pattern on multiple columns but one

How do I search for a pattern using GREP that must occur AFTER the nth occurrence of another pattern on the same line?

How to search by pattern in thunar

How to search file with pattern

how to read multiple lines in a file and put them in grep pattern?

How do I grep for multiple patterns with pattern having a pipe character?

How to grep for multiple strings in the same line inside a file with similar pattern?

How to exclude multiple directories that match a glob pattern in "grep -R"?