Link to home
Create AccountLog in
Avatar of josgood
josgoodFlag for United States of America

asked on

Linux grep regex, skip words

I want to define near as "within a specified number of words".  I want to find instances of one word near another.

As a working example, I have a file containing "provided in Section".  I want to find it by finding "provided", skipping a word, and then finding Section.  I'll expand this to a more general solution.

In Kubuntu, I'm using egrep, as in
   egrep "provided(.\w*).Section" filename
which works fine.

But
   egrep "provided(.\w*)\{1,1\}.Section" filename
does not work.  I'm confused.

I don't need to capture the intervening word.  I'll add a '+' to the '.' later.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Is this sufficient? Find Section within 4 words of provided (for example):
egrep "provided( \w*){0,3}Section" test

Open in new window

Actually, I think it needs a slight modification (missing space):
egrep "provided( \w*){0,3} Section" test

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nagarajg
nagarajg
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of josgood

ASKER

Yes, that works very well, and I see my error.  I was placing a slash before each curly brace.

Thank you.