Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to grep on three items

SOLVED
Go to Solution
Occasional Visitor

how to grep on three items

Dear All,

i have a question, i need to grep on three items i a line of a logfile

like:

grep item1 and item2 and item3 /var/log/logfile

One option is to do:

grep item1 logfile | grep item2 | grep item3

but is there a nicer way ?

Kl@@s
9 REPLIES
Honored Contributor Honored Contributor

Re: how to grep on three items

I normally use:

egrep "item1|item2|item3" logfile.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Honored Contributor Honored Contributor

Re: how to grep on three items

> but is there a nicer way ?

Almost always. "man grep":

[...]
By default, the grep command treats a pattern as a basic regular expression
(BRE). With the -E option, the pattern is treated as an extended regular
expression (ERE). With the -F option, the pattern is considered a fixed
string. See the following discussion of regular expressions.
[...]

If you know the order of the items:

bash$ echo '11a22b33c44' | grep 'a.*b.*d'
bash$ echo '11a22b33c44' | grep 'a.*b.*c'
11a22b33c44
Honored Contributor Honored Contributor

Re: how to grep on three items

> I normally use:
> [...]

Really? For this problem? AND != OR.
Honored Contributor Honored Contributor

Re: how to grep on three items

You are right Steven. I messed up the question.

egrep -e item1 -e item2 logfile
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Honored Contributor Honored Contributor

Re: how to grep on three items

> You are right Steven. I messed up the
> question.

Nah. You messed up the _answer_.

> egrep -e item1 -e item2 logfile

Twice.

bash$ echo '11a22b33c44' | egrep -e a -e d
11a22b33c44

Testing is _such_ a bother.
Honored Contributor Honored Contributor

Re: how to grep on three items

Jajajaj, you are correct, the results of my test where inaccurate.

Shame on me. Ok, last try, but let's avoid grep/egrep =)

awk '/item1/ && /item2/ { print $0 }' logfile

Tested with:

echo 11a22b33c44 | awk '/a/ && /d/ { print $0 }'
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Acclaimed Contributor Acclaimed Contributor

Re: how to grep on three items

>grep item1 logfile | grep item2 | grep item3
>but is there a nicer way?

This is doing AND. If these items are in that order you can do:
grep "item1.*item2.*item3" logfile
Acclaimed Contributor Acclaimed Contributor

Re: how to grep on three items

>if these items are in that order you can do:

Oops, Steven already said that.
You could list all of the orders with -e. :-)
Highlighted
Occasional Visitor

Re: how to grep on three items

Thanks all for the thinking.

In the meantime i've accepted the solution grep 'a.*b.*d' as for me the best option, but submitting points does not work for me at this moment due to firewall rules or something so this weekend i will do my job to thank you all the propper way!

Again thanks!