Operating System - HP-UX
1751976 Members
4544 Online
108784 Solutions
New Discussion юеВ

grep for multiple searches

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

grep for multiple searches

Hi,

can I use a single grep for multiple searches on the same input line:

ftpelp1 pts/1 Apr 27 17:32 - 18:26 (00:53)

so I want all entries with ftpelp1 and Apr 27.

I know I can do

last|grep ftpelp1 |grep "Apr 27"

but can I do like in linux:

last |grep -e ftpelp1 -e "Apr 27"

thanks


Chris.
hello
7 REPLIES 7
Steven E. Protter
Exalted Contributor
Solution

Re: grep for multiple searches

Shalom Chris,

The man page shows -e as a valid option in grep for patter matching.

Would it not be easier to just test it on HP-UX?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Aneesh Mohan
Honored Contributor

Re: grep for multiple searches

HI Chris ,

with the help of -e option you can do multiple search.


Thanks,
Aneesh
Geoff Wild
Honored Contributor

Re: grep for multiple searches

Or just use egrep


last | egrep "ftpelp1|Apr 27"

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.
James R. Ferguson
Acclaimed Contributor

Re: grep for multiple searches

Hi Chris:

...or use the '-E' option to enable extended regular expressions:

# last|grep -E "ftpelp1|Apr 27"

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: grep for multiple searches

ok thanks guys,

looks like grep does work slightly different from 1 OS to the next.

but some good examples here that I can adapt across AIX Linux Tru64 and HPUX.

cheers.

Chris
hello
OldSchool
Honored Contributor

Re: grep for multiple searches

"
I know I can do

last|grep ftpelp1 |grep "Apr 27"

but can I do like in linux:

last |grep -e ftpelp1 -e "Apr 27"
"

no...No....NO!

the results of the above are entirely different from each other. You said: "so I want all entries with ftpelp1 and Apr 27."...not a really clear problem definition.

If you want a list of all lines containing both "ftpelp1" and "Apr 27", the use the code as noted in your first example above

I you want all lines containing either (or both) "ftpelp1" or "Apr 27", then the second is sufficient....
Dennis Handly
Acclaimed Contributor

Re: grep for multiple searches

You have three ways to search for multiple patterns (OR) without going to the dark side and using egrep:
1) use -e for each
2) use -f and put them in a file
3) use a newline:
$ grep "foo
bar" file

If you want to use grep to do an AND you can use:
1) piped greps like you had.
2) big regular expressions:
$ grep "ftpelp1.*Apr 27"

If you want either order, you would need to use two -e with the words reversed.