Operating System - HP-UX
1756287 Members
2727 Online
108844 Solutions
New Discussion юеВ

Re: grep for multiple patterns

 
SOLVED
Go to solution
Rick Garland
Honored Contributor

grep for multiple patterns

Hi all:

Quick question on the use of 'grep'

I have a need to do multiple greps of processes from the command line. Each pattern will be different but I have to check for the occurance of any of these patterns.

I have tried
ps -ef | grep a b c d e
Responds that "grep can't open b c d e"

Would I need to do;
ps -ef | grep a | grep b | grep c | grep d
Or is there a better way?

Thanks!
5 REPLIES 5
RAC_1
Honored Contributor

Re: grep for multiple patterns

Not completely sure on what you want??

ps -ef | grep 'a b c d e'
ps -ef | egrep "a|b|c|d|e"

Anil
There is no substitute to HARDWORK
Geoff Wild
Honored Contributor
Solution

Re: grep for multiple patterns

egrep!

ps -ef |egrep -i 'a|b|c|d'


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Pete Randall
Outstanding Contributor

Re: grep for multiple patterns

Rick,

I think extended regular expressions (ERE) are what you're looking for:

ps -ef |grep -e "a" -e "b" -e "c" -e "d" -e "e"


Pete

Pete
H.Merijn Brand (procura
Honored Contributor

Re: grep for multiple patterns

# grep -e pat1 -e pat2 -e pat3 file

greps pat1 OR pat2 OR pat3 OR pat5

# grep pat1 file | grep pat2 | grep pat3

greps pat1 AND pat2 AND pat3

# grep -e pat1 -e pat2 file | grep pat3

greps (pat1 OR pat2) AND pat3

# perl -ne'/pat1/||/pat2/||/pat3/and print'

pat1 OR pat2 OR pat3

# perl -ne'(/pat1/||/pat2/)&&/pat3/and print'

(pat1 OR pat2) AND pat3

etc etc ...

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rick Garland
Honored Contributor

Re: grep for multiple patterns

Many thanks all!