nyaray
nyaray

Reputation: 508

grep with upper bound for middle of search pattern

I'm searching for lines with occurrences of foo and one of the numbers 5, 6 or 7, but I only want lines where they (foo and one of the mentioned numbers) are no more than 20 characters apart from each other. So far, the closest thing I've come up with, to no avail, is:

grep -rniE "foo(.*){0,20}[567]" .

I'm getting something basic wrong with my pattern, I'm sure, but I just can't see it right now.

Upvotes: 2

Views: 189

Answers (1)

devnull
devnull

Reputation: 123608

The problem is here:

(.*)

* is greedy. You wanted to match zero to twenty instances of any character. Say:

grep -rniE 'foo(.){0,20}[567]' .

Upvotes: 4

Related Questions