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!

what is your second string?

try:

grep "Smith" * | grep "second string"

Yeah I figured that out about five minutes after I posted. Thanks for replying!

or all in one...

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.

awk '/STRING1/ && /STRING2/'

True!:frowning:

you may try below perl code to find clue for your real case.

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