Operating System - HP-UX
1752805 Members
5515 Online
108789 Solutions
New Discussion юеВ

Re: Exact word search using grep

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Exact word search using grep

Hi,

I am trying to search a word and greping the output of "ps -ef|grep someword".
This grep also gives the result someword,somwordxxx, and somewordabc etc which i do not want.

Can someone suggest how to grep exact word from the output of ps -ef ?

Thanks,
Shiv
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Exact word search using grep

Hi Shiv:

If you want to match "words" with 'grep' then use the '-w' switch.

That said, if the question is really about matching a process from 'ps', use the UNIX95 (XPG4) options like:

# UNIX95= ps -C cron

Note the whitespace after 'UNIX95='. This arms the behavior only for the duration of the commandline.

You can also add options to your taste:

# UNIX95= ps -C cron -o pid,ppid,args,etime
PID PPID COMMAND ELAPSED
1780 1 /usr/sbin/cron 78-17:21:06

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Exact word search using grep

Or, if you want "someword" with white space
around it, then search for "someword" with
white space around it:

[...] | grep ' someword '

If tabs are possible as well as spaces, then
you'll need to work a little harder, but not
much harder.
Arturo Galbiati
Esteemed Contributor

Re: Exact word search using grep

Hi Shiv,
even if the best option is to use UNIX95 as already suggested a trick for you is to use:
ps -ef|grep [s]omeword
this will avoid you to add | grep -v grep to esclude the grep command itself from your ouput list.

Just my .02$
Art
Steven Schweda
Honored Contributor

Re: Exact word search using grep

> [...] | grep [s]omeword

Clever. Spaces work in there, too:

[...] | grep ' [s]omeword '

Of course, if "someword" is a variable, this
all gets harder to do.

(And if you're looking for a program name
with no arguments in "ps" output, then you
may not want that last space. If you're
looking for a UID, then you probably do.)