简体   繁体   中英

Grep Search all files in directory for string1 AND string2

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 for one word:

grep -r "db-connect.php" .

How can I extend the above to look for files that contain both "db-connect.php" AND "version".

I tried this: grep -r "db-connect.php\\|version" . but this is an OR ie it gets file that contain one or the other.

Thanks all for any help

grep -r db-connect.php . | grep version

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.

在我的cygwin中,给定的答案不起作用,但以下是:

grep -l firststring `grep -r -l secondstring . `

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

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

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?

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

使用具有多行匹配的PCRE(Perl兼容正则表达式)并返回包含两个字符串( AND而不是OR )的文件的文件名。

grep -Plr '(?m)db-connect\.php(.*\n)*version|version(.*\n)*db-connect\.php' .

为什么坚持只有grep:

perl -lne 'print if(/db-connect.php/&/version/)' *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM