LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-09-2010, 11:11 AM   #1
Fliggerty
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
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)' {} \;
 
Old 12-09-2010, 11:24 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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]

Last edited by Tinkster; 12-09-2010 at 11:42 AM.
 
Old 12-09-2010, 11:47 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Um... couldn't you just use
Code:
grep -iE "(localhost|DATABASE_NAME)" *
?

Last edited by Snark1994; 12-09-2010 at 11:48 AM. Reason: Replaced search text with the desired strings
 
Old 12-09-2010, 12:24 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Snark1994 View Post
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
 
Old 12-10-2010, 09:40 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Very sorry, I misread your original question...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple strings GEEXTER Linux - General 7 12-06-2013 10:56 PM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 03:40 PM
grep two strings Hondro Linux - General 3 09-08-2008 10:55 PM
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 08:46 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 04:13 PM


All times are GMT -5. The time now is 12:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration