Operating System - HP-UX
1829669 Members
9759 Online
109992 Solutions
New Discussion

How to find exact string match

 
SOLVED
Go to solution

How to find exact string match

Hi pals,

Here is my requirement.

I am running "ps -ef" command and I need to filter out ONLY a required process information. For example, I need to filter out "man" process details.
If I use grep to do this job, it filters out man process information along with other strings contain this pattern ( eg., manager ).
I don't want any of these unnecessary outputs. I need only process information about man process. How to do it?

Thanks in advance.

With warm regards,
Jegi,
NBC
8 REPLIES 8
Rita C Workman
Honored Contributor

Re: How to find exact string match

This (at least to me) sounds a bit tricky...

What you might be able to do is grep for what you want and then pipe down to eliminate what you don't want. This will be difficult to set up for because (at least here...) different processes may start running that you hadn't accounted for....but...

ps -ef | grep man | grep -v -e 'manager' -v -e 'management' -v -e 'managed' and so forth...

Just a thought,
rcw
Richard Darling
Trusted Contributor
Solution

Re: How to find exact string match

if it is only the word man surrounded by whitespace...why not
grep -v ' man '

placing a space before and after man in the quotes?

RD

Re: How to find exact string match

Thanks friends,

Richards reply gave me an idea. Here is what i did and got what i needed.

1) ps -ef|grep man > /tmp/ps.out
2) vi /tmp/ps.out
3) navigate through the file to identify what kind of character( whether white space or TAB) preceeds and succeeds "man" entry.
4) found process name is preceeded by white space and succeeded by NOTHING.
5) run ps -ef|gre ' man' and obtained desired output.

Thanks a lot
Regards,
Jegi,
NBC


Re: How to find exact string match

Dear friends,

Procedure i mentioned earlier may not work at all the times. But using white space surely gives a hand in searching for exact string (awk might assist to attain desired result)



Thanks
Jegi
Bill Hassell
Honored Contributor

Re: How to find exact string match

There i9s a little known feature of ps called XPG4 or UNIX95 behavior. When you set the UNIX95 variable (to anything including null), the -o option becomes active, and that means a do-it-yourself version of ps.

For example:

Show just the process ID, the memory used and the command (ie, process name) without any leading directories:

UNIX95= ps -e -o 'pid,vsz,comm'

This makes parsing much easier.


Bill Hassell, sysadmin
Dan Hetzel
Honored Contributor

Re: How to find exact string match

Hi,

If you put the following lines in a script and make it executable, it would save you retyping the full stuff every time.

#!/usr/bin/sh
# get process entry for name given as argument
ps -ef | sed -n "/[[:space:]]$1[[:space:]]*/p"

It is using the regular expression [[:space:]] to allow for non printable characters around your word.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Andre Lattier
New Member

Re: How to find exact string match

the gnu grep has a '-w' option which searchs for word (smart : you don't have to think about end of line, tab, ...)
Joseph C. Denman
Honored Contributor

Re: How to find exact string match

Good one bill.

Learn something new everyday!!!

Thanks,

jcd
If I had only read the instructions first??