1834150 Members
2350 Online
110064 Solutions
New Discussion

Re: grep question

 
SOLVED
Go to solution
Mike_Ca Li
Regular Advisor

grep question

ps -ef | grep -v grep | grep "SVR"

SVR
SVR1
SVR2
SVR4
SVR5

How to grep "SVR" so that it returns only
SVR ? Thank you.
16 REPLIES 16
Steven E. Protter
Exalted Contributor

Re: grep question

ps -ef | grep -i grep | grep "SVR "
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
A. Clay Stephenson
Acclaimed Contributor

Re: grep question

If you were reading from a file, you would anchor the regular expression
grep -E -e '^SVR$' but since you are grepping the output of ps I would look for ' SVR ' (spaces surrounding the pattern).

ps -ef | grep -v grep | grep -E -e ' SVR '
If it ain't broke, I can fix that.
Victor Fridyev
Honored Contributor

Re: grep question

Hi,

I'm not sure that you can do this with grep.
If you are sure that the pattern there is on the end of string, you can use SVR$, or, if you are sure that after the pattern there is a space, use "SVR ".
IMHO, the best way is
ps -ef |awk '$8=="SVR" {print}'

HTH
Entities are not to be multiplied beyond necessity - RTFM
Mel Burslan
Honored Contributor

Re: grep question

ps -ef | grep -v grep | grep "SVR" | grep -v [0-9]

if the suffix is always going to benumerals, this should do the trick.
________________________________
UNIX because I majored in cryptology...
Rick Garland
Honored Contributor

Re: grep question

The -x switch to grep will return exactly.

ps -ef | grep -x SVR
Greg Vaidman
Respected Contributor

Re: grep question

the output you list can't be the output of the command you list, since "ps -ef" generates several columns of output.

in any case, I think you want something like:

grep -e "SVR[[:space:]]" -e "SVR$"

at the end of your command - this will prevent SVR1, SVR2, etc., from being displayed. this searches for SVR followed by either whitespace or end-of-line, which should get you what you want. it won't prevent something like ASVR or BSVR, however, but I'm guessing that won't be an issue in your case.

--Greg
Victor Fridyev
Honored Contributor

Re: grep question

Hi,

The switch -x works for exact string, i.e if you have only SVR in the string. For a string, which contain SVR as a word -x does not work. 8((((
Entities are not to be multiplied beyond necessity - RTFM
Rick Garland
Honored Contributor

Re: grep question

Whoops, take that back, no -x switch
MarkSyder
Honored Contributor

Re: grep question

Sorry to put a damper on Mel's solution (grep -v [0-9]), but isn't the output of ps -ef always going to contain a number in in the PID?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Mel Burslan
Honored Contributor

Re: grep question

oops.. my bad
ps -ef | grep -v grep | grep "SVR" | grep -v SVR[0-9]

________________________________
UNIX because I majored in cryptology...
Geoff Wild
Honored Contributor

Re: grep question

does it always show up first in the list?

ps -ef |grep SVR |head -1

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.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: grep question

Get GNU grep if you don't have it yet!

# grep -w SVR

-w, --word-regexp force PATTERN to match only whole words

Get GNU grep if you don't have it yet!
Get GNU grep if you don't have it yet!
Get GNU grep if you don't have it yet!

Enjoy, Have FUN! H.Merijn

PS. If you build it yourself from scratch, install pcre first, and you get a grep that optionally supports Perl Compatible Regular Expressions!

Enjoy, Have More FUN! (TM) H.Merijn
Enjoy, Have FUN! H.Merijn
Bill Hassell
Honored Contributor

Re: grep question

Using grep and ps together is a very unstable combination. grep does not care at all about what matches on the line. It could a user name, a process name or it might be part of the command line argument list. If you are looking for a specific process named SVR, then the solution is trivial (no grep at all):

UNIX95= ps -fC SVR

Type the command as shown. The UNIX95= is a one line method to set a variable temporarily. In the man page for ps, you'll see a number of options (-C -H -o) that are "XPG4" options. But they are disabled when UNIX95 does not exist. You don't want to set UNIX95 on a separate line as it affects other commands and libraries. So use the above example to find just SVR (or just SVR1, etc). The -C is an exact process name match built into ps.

A good rule of thumb is to always avoid using grep and ps together. If you need to find a list of processes by PID, use the -p option. Processes by owner: -u option. Create a customized output from ps: use -o

Here's a command line that shows only processes named sh (not ksh, not csh, not hasdaemon, not username trish, etc) and shows just user, PID and PPID, process size in Kbytes, and the command line for each process:

UNIX95= ps -C sh

UNIX95=


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

Re: grep question

Oops, the last line should read:

UNIX95= ps -C sh -o user,pid,ppid,vsz,args

The reason that grep fails is that it has no concept of field matching...it's just a left to right comparison on each line.


Bill Hassell, sysadmin
Tim D Fulford
Honored Contributor

Re: grep question

what about perl.... it has one of the best regular expression engines available PLUS boolean logic ... etc .. To demonstaate here is what I'd do to print out the line AND the headder whilst removing the perl command itself...

ps -fe | perl -ane 'print if ( ! m/perl.*SVR[0-9]*/ && (m/SVR[0-9]*/ || m/PID/ ))'

UID PID PPID C STIME TTY TIME COMMAND
kilbours 25025 1 0 May 5 ? 0:00 SVR
kilbours 10393 1 0 Mar 1 ? 0:00 SVR1
kilbours 10393 1 0 Mar 1 ? 0:00 SVR199
kilbours 10393 1 0 Mar 1 ? 0:00 SVR0

A few pointers about how flexible perl is ".*" mean match anything 0 or more times "[0-9]*" means match any number 0 or more times. So the above says

o ! m/perl.*SVR[0-9]* DONT print if there is a line that starts perl and has SVR after it so ???perlSVR??? would do, and so would perl xxxxxxxxx 1234 SVR yyyy etc.
o m/SVR[0-9]*/ DO print if you see a line with SVR ending with any number of numerals e.g ???SVR??? or ????SVR0???? or ???SVR1???
o m/PID/ Do print if you see a line with PID in it (the headder for example)

Regards

Tim
-
Tim D Fulford
Honored Contributor

Re: grep question

oops .. I'm going way off at a tangent trying to show how flexible perl is and not answering your specific question here..

# ps -fe | perl -ane 'print if ( ! m/perl.*SVR// && m/\bSVR\b/)'

kilbours 25025 1 0 May 5 ? 0:00 SVR

This will work, but it will also match "SVR -o multithread", but NOT "SVR1 -o multithread" If you also want to get rid of the "-o multithread" bit then

# ps -fe | perl -ane 'print if ( ! m/perl.*SVR/ && m/\bSVR$/)'

It is also usual to only want, say the PID & cmd for this you can use

ps -fe | perl -ane 'print "$F[1] $F[-1]\n " if ( ! m/perl.*SVR/ && m/\bSVR$/ )'

Regards

Tim
-