Grep two words next to each other

Let's say I have a line in the file "This is perhaps the easiest place to add new features." and I want to lie two words close together. I do

grep -ERHn "\beasiest\W+(?:\w+\W+){1,6}?place\b" *

which works and gives me a string. But when I do

grep -ERHn "\beasiest\W+(?:\w+\W+){1,10}?new\b" *

does he fail by defeating the whole point {1,10}? This list is listed on regular-expression.info, as well as in several Regex books. Although they do not describe it with grep, it does not matter.

Update

I put regex in a python script. It works, but has nothing good grep -C ...

#!/usr/bin/python
import re
import sys
import os

word1 = sys.argv[1]
word2 = sys.argv[2]
dist = sys.argv[3]
regex_string = (r'\b(?:' 
    + word1  
    + r'\W+(?:\w+\W+){0,'
    + dist
    + '}?'
    + word2 
    + r'|'
    + word2
    + r'\W+(?:\w+\W+){0,'
            + dist
    + '}?'
    + word1
    + r')\b')

regex = re.compile(regex_string)


def findmatches(PATH):
for root, dirs, files in os.walk(PATH):
    for filename in files:
        fullpath = os.path.join(root,filename)

        with open(fullpath, 'r') as f:
            matches = re.findall(regex, f.read())
            for m in matches:
                print "File:",fullpath,"\n\t",m

if __name__ == "__main__":  
    findmatches(sys.argv[4])    

Call

python near.py charlie winning 6 path/to/charlie/sheen

works for me.

+3
source share
2 answers

Do you really need a look structure? Perhaps this is enough:

grep -ERHn "\beasiest\W+(\w+\W+){1,10}new\b" * 

Here is what I get:

echo "This is perhaps the easiest place to add new functionality." | grep -EHn "\beasiest\W+(\w+\W+){1,10}new\b"

( ): 1: , , .

Edit

:

, .bashrc:

grepNear() {
 grep -EHn "\b$1\W+(\w+\W+){1,10}$2\b"
}.

bash: echo "..." | grepNear easiest new

+1

grep , Python. - (?:\w+\W+), grep ?, :, \w+, , \w+. ? grep, , ( , [?] ).

? :

$ cat file
This is perhaps the easiest place to add new functionality.

grep , :

$ grep -ERHn "\beasiest\W+(?:\w+\W+){1,10}?new\b" file

:

$ cat file2
This is perhaps the easiest ?:place ?:to ?:add new functionality.

, ?:. :

$ grep -ERHn "\beasiest\W+(?:\w+\W+){1,10}?new\b" file2
file2:1:This is perhaps the easiest ?:place ?:to ?:add new functionality.

, ?: :

$ grep -ERHn "\beasiest\W+(\w+\W+){1,10}?new\b" file
file:1:This is perhaps the easiest place to add new functionality.

, ( , ), .

: {1,10} {0,10} ?:

$ grep -ERHn "\beasiest\W+(\w+\W+){0,10}new\b" file
file:1:This is perhaps the easiest place to add new functionality.
0

Source: https://habr.com/ru/post/1796637/


All Articles