Abs
Abs

Reputation: 57966

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 i.e. it gets file that contain one or the other.

Thanks all for any help

Upvotes: 19

Views: 44057

Answers (8)

Coroos
Coroos

Reputation: 390

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.

Upvotes: 4

Vijay
Vijay

Reputation: 67319

Why to stick to only grep:

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

Upvotes: 0

moinudin
moinudin

Reputation: 138497

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

Upvotes: 23

incircuitous
incircuitous

Reputation: 141

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

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

Upvotes: 3

tchrist
tchrist

Reputation: 80443

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?

Upvotes: 3

Dennis Williamson
Dennis Williamson

Reputation: 360683

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' .

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247210

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

Upvotes: 1

jzrk
jzrk

Reputation: 295

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

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

Upvotes: 3

Related Questions