Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-31-2005
Registered User
 
Join Date: Oct 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
How do I search for 2 strings (AND operator) ?

I have a need to search for files containing 2 strings as in (AND operator). No one at my site seems to know if it is possible. There is only documentation for the "or' operator.

I know I can do a search, copy all the matched files into a temp directory & do the second search in the temp directory.

Thanks.
Sponsored Links
    #2  
Old 10-31-2005
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,740
Thanks: 181
Thanked 662 Times in 638 Posts

Code:
# search for 2 strings
grep "string1" /path/to/files/* | grep "string2"

try that.
Sponsored Links
    #3  
Old 10-31-2005
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,740
Thanks: 181
Thanked 662 Times in 638 Posts
or

Code:
find /path -name \*.lis -exec grep "string1" {} \; | grep "string2"

    #4  
Old 11-01-2005
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Location: 54.23, -4.53
Posts: 1,792
Thanks: 1
Thanked 101 Times in 91 Posts
Or maybe....
Code:
awk '/string1/ && /string2/' files*

Sponsored Links
    #5  
Old 11-02-2005
Registered User
 
Join Date: Oct 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Jim Mcnamara

I tried your command but it works like the "OR" command. I got hits with either one match.

When I get a hit on a file, I need to see string1 & string2 contain in that file.

Kartheg.
Sponsored Links
    #6  
Old 11-02-2005
Registered User
 
Join Date: Jan 2005
Posts: 683
Thanks: 0
Thanked 4 Times in 4 Posts
Jim's command filters out lines where the string1 exists and then passes this text through another filter to find string2. The two greps are an AND. Post some sample text for review.
Sponsored Links
    #7  
Old 11-02-2005
Registered User
 
Join Date: Oct 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
R u saying string1 & string2 should exist on the same line to get a hit ?. In my case string1 & string2 are on different lines. I only tried the first example.

Kartheg.

Last edited by Kartheg; 11-02-2005 at 01:44 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to avoid 'unexpected operator' error when comparing 2 strings hakermania Shell Programming and Scripting 9 08-09-2010 08:16 AM
Search between 2 strings lavascript Shell Programming and Scripting 3 02-16-2010 09:02 AM
how to search with 2 strings. syamkp Shell Programming and Scripting 2 03-10-2008 04:50 AM
Search between strings with an OR pbsrinivas Shell Programming and Scripting 3 07-12-2007 04:46 AM
Search for strings lesstjm UNIX for Dummies Questions & Answers 5 04-26-2002 09:58 AM



All times are GMT -4. The time now is 12:24 AM.