Re: how to grep on three items (956 Views)
Reply
Occasional Visitor
Klaas Eenkhoorn
Posts: 2
Registered: ‎03-05-2009
Message 1 of 10 (956 Views)
Accepted Solution

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
Honored Contributor
Ivan Ferreira
Posts: 6,957
Registered: ‎05-07-2004
Message 2 of 10 (956 Views)

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
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 3 of 10 (956 Views)

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
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 4 of 10 (956 Views)

Re: how to grep on three items

> I normally use:
> [...]

Really? For this problem? AND != OR.
Honored Contributor
Ivan Ferreira
Posts: 6,957
Registered: ‎05-07-2004
Message 5 of 10 (956 Views)

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
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 6 of 10 (956 Views)

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
Ivan Ferreira
Posts: 6,957
Registered: ‎05-07-2004
Message 7 of 10 (956 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 8 of 10 (956 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 9 of 10 (956 Views)

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. :-)
Occasional Visitor
Klaas Eenkhoorn
Posts: 2
Registered: ‎03-05-2009
Message 10 of 10 (956 Views)

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!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.