Grep Search all files in directory for string1 AND string2

前端 未结 8 418
迷失自我
迷失自我 2020-12-31 02:22

How can I make use of grep in cygwin to find all files that contain BOTH words.

This is what I use to search all files in a directory recursively fo

相关标签:
8条回答
  • 2020-12-31 02:45

    If you want to grep for several strings in a file which have different lines, use the following command:

    grep -rl expr1 | xargs grep -l expr2 | xargs grep -l expr3
    

    This will give you a list of files that contain expr1, expr2, and expr3.

    Note that if any of the file names in the directory contains spaces, these files will produce errors. This can be fixed by adding -0 I think to grep and xargs.

    0 讨论(0)
  • 2020-12-31 02:47

    In my cygwin the given answers didn't work, but the following did:

    grep -l firststring `grep -r -l secondstring . `
    
    0 讨论(0)
  • 2020-12-31 02:48

    grep "db-connect.php" * | cut -d: -f1 | xargs grep "version"

    I didn't try it in recursive mode but it should be the same.

    0 讨论(0)
  • 2020-12-31 02:49

    The uses PCRE (Perl-Compatible Regular Expressions) with multiline matching and returns the filenames of files that contain both strings (AND rather than OR).

    grep -Plr '(?m)db-connect\.php(.*\n)*version|version(.*\n)*db-connect\.php' .
    
    0 讨论(0)
  • 2020-12-31 02:57

    Do you mean "string1" and "string2" on the same line?

    grep 'string1.*string2'
    

    On the same line but in indeterminate order?

    grep '(string1.*string2)|(string2.*string1)'
    

    Or both strings must appear in the file anywhere?

    grep -e string1 -e string2
    
    0 讨论(0)
  • 2020-12-31 03:07

    To and together multiple searches, use multiple lookahead assertions, one per thing looked for apart from the last one:

    instead of writing

    grep -P A  * | grep B
    

    you write

    grep -P '(?=.*A)B' *
    
    grep -Pr '(?=.*db-connect\.php)version' .
    

    Don’t write

    grep -P 'A.*B|B.*A' *
    

    because that fails on overlaps, whereas the (?=…)(?=…) technique does not.

    You can also add in NOT operators as well. To search for lines that don’t match X, you normally of course use -v on the command line. But you can’t do that if it is part of a larger pattern. When it is, you add (?=(?!X).)*$) to the pattern to exclude anything with X in it.

    So imagine you want to match lines with all three of A, B, and then either of C or D, but which don’t have X or Y in them. All you need is this:

    grep -P '(?=^.*A)(?=^.*B)(?=^(?:(?!X).)*$)(?=^(?:(?!Y).)*$)C|D' *
    

    In some shells and in some settings. you’ll have to escape the ! if it’s your history-substitution character.

    There, isn’t that pretty cool?

    0 讨论(0)
提交回复
热议问题