Operating System - HP-UX
1834302 Members
2198 Online
110066 Solutions
New Discussion

grep something AND something

 
SOLVED
Go to solution
Ratzie
Super Advisor

grep something AND something

I am trying to grep a file, that has this AND that.

grep "s" && "oracle"

does not seem to work.
11 REPLIES 11
Sridhar Bhaskarla
Honored Contributor

Re: grep something AND something

Hi,

Your question isn't quite clear to me. If you are searching for lines that contain both s and oracle, a simple way is

grep "oracle" file|grep "s"

If you are looking for lines that contain either s or oracle or both then

grep -E -i 'oracle|s' file

or

grep -i -e 'oracle' -e 's' file

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Pete Randall
Outstanding Contributor
Solution

Re: grep something AND something

How about

grep "s" |grep "oracle"

?


Pete

Pete
Ratzie
Super Advisor

Re: grep something AND something

I have tried that, but I need it to contain BOTH!
Sridhar Bhaskarla
Honored Contributor

Re: grep something AND something

Can you give us an example?

$cat data

oracle a fine word
s is a letter
s not there in oracle
ofcourse s is not there in oracle
but o in oracle

$grep oracle data |grep s
s not there in oracle
ofcourse s is not there in oracle

??

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Uday_S_Ankolekar
Honored Contributor

Re: grep something AND something

If you are trying to match multiple patterns then use -e option in grep

Good Luck..
Pete Randall
Outstanding Contributor

Re: grep something AND something

As Sri points out, the pipe construct ( grep "s" | grep "oracle" ) should give you what you're looking for. Can you provide details of how/where you think it's failing?


Pete

Pete
Stuart Abramson_2
Honored Contributor

Re: grep something AND something

He's trying to do a grep with an AND.

Give me the lines where BOTH "s" and "oracle" appear in the same line.

grep ("s" -a "oracle") file

I can't think of any way to do it!
Michael Schulte zur Sur
Honored Contributor

Re: grep something AND something

Hi,

you can try:
grep -E ".*text1.*text2|.*text2.*text1" file

Michael
Hein van den Heuvel
Honored Contributor

Re: grep something AND something

Right...
The leading '.*' for 'any characters' do not seem to be needed, giving:

grep -E "oracle.*s|s.*oracle"

but normally you grab for a double grep...

Hein.
Chris Hulihan
Advisor

Re: grep something AND something

Sri's answer is right -- I do this all the time ..
grep -i -e 'oracle' -e 's' file

Note -- BOTH THINGS YOU ARE LOOKING FOR *MUST* have the -e in front of it. That screwed me up before I got the hang of it and I think this could be what you are missing. The following command does not work because it's missing the other "-e":
grep -i oracle -e file

(Note, the -i is optional so that it's case-insensitive)



Todd McDaniel_1
Honored Contributor

Re: grep something AND something

Here is my test file 3 lines

#cat toddfile
s and oracle
s only
oracle only

Here is my test...

root:/root
# grep s /tmp/toddfile
s and oracle
s only

root:/root
# grep -e s -e oracle /tmp/toddfile
s and oracle
oracle only
s only

root:/root
# grep s |grep oracle /tmp/toddfile
s and oracle
oracle only

root:/root
# grep s /tmp/toddfile |grep oracle
s and oracle


See the last one shows your example of only showing those lines with both s and oracle on them...

Hope this helps.
Unix, the other white meat.