How do I search for 2 strings (AND operator) ? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

How do I search for 2 strings (AND operator) ?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-31-2005
Kartheg Kartheg is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 24 November 2012, 9:58 PM EST
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
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2014, 4:09 PM EDT
Location: NM
Posts: 10,188
Thanks: 275
Thanked 785 Times in 734 Posts

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

try that.
Sponsored Links
    #3  
Old 10-31-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2014, 4:09 PM EDT
Location: NM
Posts: 10,188
Thanks: 275
Thanked 785 Times in 734 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
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 111 Times in 98 Posts
Or maybe....
Code:
awk '/string1/ && /string2/' files*

Sponsored Links
    #5  
Old 11-02-2005
Kartheg Kartheg is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 24 November 2012, 9:58 PM EST
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
tmarikle tmarikle is offline Forum Advisor  
Registered User
 
Join Date: Jan 2005
Last Activity: 27 September 2011, 12:36 PM EDT
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
Kartheg Kartheg is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 24 November 2012, 9:58 PM EST
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 10:08 PM.