Avatar of oxygen_728
oxygen_728 asked on

Linux - Grep - How do I search for two terms at once?

I want to search for all files that contain both "password" and "_PUT"

How would i do this on a grep search?

Thanks

LinuxLinux OS Dev

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
Kent Olsen

Hi oxygen_728,

On the same line or anywhere in the file?



Kent
ASKER
oxygen_728

I'd prefer on the same line,

but i'd like to know how to do it in the same file too

Thanks for your time
omarfarid

put the different strings in a file e.g. myfile

string1
string2

then use grep -f myfile *
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
oxygen_728

Omar, does that search for them on the same line?

Do I put quotes around my strings?

Does that search for both terms using an AND conjunction or an OR conjunction?

Thanks
omarfarid

no, this is an OR so either string1 or string2 or .....

if in the same line the use

string1*string2
ASKER
oxygen_728

Omar, I cannot seem to get that to work

can you provide a more detailed example?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
omarfarid

also,you can do

grep string1 * | grep string2 | grep string3

I could not do it with string1*string2
SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
oxygen_728

Ghost, is there some way to alternatively check a file for both of those that don't necessarily occur on the same line?

Thanks
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

grep -l password `grep -l _PUT *`
Your help has saved me hundreds of hours of internet surfing.
fblack61