LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep -- finding files that contain two separate strings (https://www.linuxquestions.org/questions/linux-newbie-8/grep-finding-files-that-contain-two-separate-strings-849297/)

Fliggerty 12-09-2010 10:11 AM

Grep -- finding files that contain two separate strings
 
Hey all,

I've got a quick grep question. I'm trying to work out a command I can use to locate all of the files in a directory that have sql database connection details. I want to do it by looking for the strings "localhost" and the name of the database.

This command isn't returning anything at all, so I'm hoping for suggestions please!

find . -type f -exec grep -l -E '^(localhost|DATABASE_NAME)' {} \;

Tinkster 12-09-2010 10:24 AM

Do you expect those words to be at the beginning of a line?

If the answer is no - there's your problem.

[edit]
That said: you're saying you want files that have BOTH.
You're looking for files that have either with the grep.
You'd either need two greps, or one very convoluted grep,
or you could use awk instead.... two greps w/ find may
be a tad ugly.

Code:

awk '/localhost/ && /DATABASE_NAME/'
[/edit]

Snark1994 12-09-2010 10:47 AM

Um... couldn't you just use
Code:

grep -iE "(localhost|DATABASE_NAME)" *
?

Tinkster 12-09-2010 11:24 AM

Quote:

Originally Posted by Snark1994 (Post 4186070)
Um... couldn't you just use
Code:

grep -iE "(localhost|DATABASE_NAME)" *
?

He could use a single grep for all files in a directory,
or a recursive grep, indeed.

But the matching still wouldn't find only files that have
BOTH strings. That said: finding all files w/ both strings
not necessarily on the same line would be a bit more
challenging than this ...



Cheers,
Tink

Snark1994 12-10-2010 08:40 AM

Very sorry, I misread your original question...


All times are GMT -5. The time now is 01:36 AM.