user2809888
user2809888

Reputation:

how to grep for the first instance of the pattern among multiple patterns

I have three different patterns and I want to find the out which patters appeared first in the file and which parrent appear last in the file and I need to print the lines also which contains the first and last pattern.

I treid below grep but its applicable only to one pattern. I think once its figured out how to find the rank of patterns then printing of last patter could be done by same logic by using "tac".

grep -m 1 "pattern1" test.txt

my three patterns are

1.PATTERN1  
2.PATTERN2  
3.PATTERN3  

Line1 this is a sample line for example without  any meaning please ignore
Line2 only meant for giving an example PATTERN2 to make my query clear to all
Line3 this is a sample line for example without  any meaning please ignore
Line4 only meant for giving an example pattern1 to make my query clear to all
Line5 this is a sample line for example without  any meaning please ignore
Line6 only meant for giving an example pattern1 to make my query clear to all
Line7 this is a sample line for example without  any meaning please ignore
Line8 only meant for giving an example pattern2 to make my query clear to all
Line9 this is a sample line for example without  any meaning please ignore
Line10 only meant for giving an example pattern3 to make my query clear to all
Line11 only meant for giving an example pattern1 to make my query clear to all

I want to print the line containg the first occarance of any patterns among PATTERN1,PATTERN2, PATTERN3.

So desired output should be :

First pattern among the three
-------------------------------
Line2 only meant for giving an example PATTERN2 to make my query clear to all

Last instance amoung the three:
-------------------------------
Line11 only meant for giving an example pattern1 to make my query clear to all

Upvotes: 1

Views: 8079

Answers (2)

yeri63
yeri63

Reputation: 1

Use a for loop instead of the grep -E construct alone. With grep -m1, only the first match is returned for each pattern. The -i option was added to ignore case, otherwise only exact matches are shown.

for i in PATTERN1 PATTERN2 PATTERN3; do grep -i $i -m1 test.txt; done

This produces the following results...

Line4 only meant for giving an example pattern1 to make my query clear to all
Line2 only meant for giving an example PATTERN2 to make my query clear to all
Line10 only meant for giving an example pattern3 to make my query clear to all

Upvotes: 0

devnull
devnull

Reputation: 123508

You can say:

grep -E -m1 "pattern1|pattern2|pattern3" test.txt

This would print the first line matching either pattern1, pattern2 or pattern3.

As you mentioned, you could use tac to find the last matching pattern in the file:

grep -E -m1 "pattern1|pattern2|pattern3" <(tac test.txt)

In case, you version of grep doesn't support -E, you could say:

grep -m1 "pattern1\|pattern2\|pattern3" test.txt

EDIT: In order to find only the first and line line matching any pattern, you could say:

grep "pattern1\|pattern2\|pattern3" test.txt | sed -n '1p;$p'

(Use the -i option for grep if you want to perform case-insensitive matches.)

Upvotes: 2

Related Questions