Grepping for two strings that MUST exist on the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping for two strings that MUST exist on the same line
# 1  
Old 06-01-2009
Grepping for two strings that MUST exist on the same line

Trying to find a way to grep for two names on a line. Both names must appear on the same line so '|' / OR is out.

So far, I'm just messing around and I've got

find . -name "*" | xargs grep "Smith"

Let me explain. I'm at a top level and need to know all the names of the files that contain the string. I've only got one name because I know I can take that and then run it through looking for the second name in a separate file. I'd like to eliminate that second step and do it all at once.

Thanks!
# 2  
Old 06-01-2009
what is your second string?

try:
Code:
grep "Smith" * | grep "second string"

# 3  
Old 06-01-2009
Yeah I figured that out about five minutes after I posted. Thanks for replying!
# 4  
Old 06-01-2009
or all in one...
Code:
grep  'Smith.*second_string'  ...

# 5  
Old 06-01-2009
Quote:
Originally Posted by JerryHone
or all in one...
Code:
grep  'Smith.*second_string'  ...

Well assuming this is not meant to be order specific that won't work.

A more general way without using an extra pipe could be awk.

Code:
awk '/STRING1/ && /STRING2/'

# 6  
Old 06-01-2009
Quote:
Well assuming this is not meant to be order specific that won't work
True!Smilie
# 7  
Old 06-02-2009
you may try below perl code to find clue for your real case.

Code:
my @arr=('str1 str2','str2 str1');
map { print $_,"\n" if /(?=.*str1)(?=.*str2)/ } @arr;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping multiple strings from one column

I have 3-column tab separated data that looks like the following: act of+n-a-large+vn-tell-v 0.067427 act_com of+n+n-a-large-manufacturer-n 0.129922 act-act_com-com in+n-j+vn-pass-aux-restate-v 0.364499666667 com nmod+n-j+ns-invader-n 0.527521 act_com-com obj+n-a-j+vd-contribute-v 0.091413... (2 Replies)
Discussion started by: owwow14
2 Replies

2. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

3. Shell Programming and Scripting

Grepping Multiple Strings on the Same Line 'Or'

I've got this command that I've been using to find strings on the same line, say I'm doing a search for name: find . -name "*" | xargs grep -i "Doe" | grep -i "John" > output.txt This gives me every line in a file that has John and Doe in it. I'm looking to add a OR operator for the second... (5 Replies)
Discussion started by: Rally_Point
5 Replies

4. Shell Programming and Scripting

Grepping for Exact Strings

ok, apparently this is a very difficult question to answer based on my searches on google that came up fruitless. what i want to do is grep through a file for words that match a specified string. but the thing is, i keep getting all words in the file that have the string in them. say for... (27 Replies)
Discussion started by: SkySmart
27 Replies

5. UNIX for Dummies Questions & Answers

Help with grepping and line number

I need help with extracting data from a large file ~900mb. Below is how the data looks like, line number value 1001 10000 ... ... 5001 50000 6001 60000 ... ... 10001 100000 ... ... 100001 ... (3 Replies)
Discussion started by: shabs1985
3 Replies

6. Shell Programming and Scripting

Trouble grepping for multiple strings

I am having a heck of a time trying to write a script that will grep for multiple strings in a single file. I am really at my wits end here and I am hoping to get some feedback here. Basic information: OS: Solaris 9 Shell: KSH Oracle Database server I was trying to grep through a file... (5 Replies)
Discussion started by: thecoffeeguy
5 Replies

7. Shell Programming and Scripting

reading line by line and grepping

I've got a file which I am reading line by line (using read line) into a variable. I then want to do a grep on that line to check for something. I've tried a number of methods none of which seem to work. I thought I had it with the code below but for some reason it doesn't like it and comes... (4 Replies)
Discussion started by: QueryMaster
4 Replies

8. Shell Programming and Scripting

Grepping 1 line above and below pattern

I have a pattern:: xldn3176bap>arj SOCRATES_MAIN_LNX | grep " FA " 10/04/2007 21:01 10/04/2007 21:01 FA 1776752/1 1 I want the line above this line and the line below it too. Can anyone tell me how it can be done? - iAm4Free (4 Replies)
Discussion started by: iAm4Free
4 Replies

9. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies

10. UNIX for Dummies Questions & Answers

grepping for something but excluding something else in the line

Ok heres the situation. I'm grepping for all running processes with the name system. but there are also processes running with the name systema. How do I just search for processes running just under the "system" user Thanks in advance (1 Reply)
Discussion started by: fusion99
1 Replies
Login or Register to Ask a Question

Featured Tech Videos