1833007 Members
2868 Online
110048 Solutions
New Discussion

grep colums

 
SOLVED
Go to solution
tom quach_1
Super Advisor

grep colums

Hi All,

Please help!
my grep sometime returned with 8 or 9 columns as below.
how to change my scripts so the return will be more consistent
my gold is to get "pmon_PO"

#ps -ef|grep pmon
oracle 27676 1 0 Jun 3 ? 14:08 ora_pmon_PO


RETURN=`ps -ef|grep [p]mon_PO|awk '{print $9}'`
if [ "ora_pmon_$DB" = "$RETURN" ]
then
exit 0
else

Thank for your help.
Tom,
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: grep colums

Hi Tom:

Use 'awk' to match and pluck the last field:

# RETURN=$(ps -ef|awk '/pmon_PO/ {print $NF}')
...

For that matter, since you are matching the basename of a process and just want the pid (if any), do:

# PID=$(UNIX95= ps -C ora_pmon_PO -opid=)

Note the whitespace after the 'UNIX95='. This arms the XPG4 behavior only for the duration of the command line.

Regards!

...JRF...
tom quach_1
Super Advisor

Re: grep colums

Thanks James,
it works,

#ps -ef|awk ' /pmon_PO/ {print $NF}'
ora_pmon_OP
$NF}

but sometimes it returned with "$NF"
how can i get rid of it.

Thanks very much,
Tom
James R. Ferguson
Acclaimed Contributor

Re: grep colums

Hi (again) Tom:

> but sometimes it returned with "$NF"
how can i get rid of it.

Yes, indeed this can happen and I should have accounted for it in my original post. Mea culpa.

Exercise this several times and occasionally you will see the extra line representing the 'awk' process we launched:

# ps -ef|awk '/cron/ {print $0,$NF}'
root 1555 1 0 Mar 3 ? 0:33 /usr/sbin/cron /usr/sbin/cron
root 20538 11785 1 18:07:41 pts/0 0:00 awk /cron/ {print $0,$NF} $0,$NF}

We don't want the last field of the second line!

We can do:

# ps -ef|awk '/cron/ && !/awk/ {print $NF}'

...which is the same rationale you see for command like:

# ps -ef|grep cron|grep -v cron

This all underscores, the value of the 'UNIX95' behavior I suggested where you exactly match a process basename ('-C ').

Regards!

...JRF...

tom quach_1
Super Advisor

Re: grep colums

Hi James,

thanks for your help,
i add the "grep at the end and it seems good enough for me.
Thanks again,
Tom

James R. Ferguson
Acclaimed Contributor

Re: grep colums

Hi (again) tom:

> i add the "grep at the end and it seems good enough for me.

Don't mix 'grep' and 'awk' in a pipelined command. DOing so adds and extra process that most often can be subjugated into 'awk':

Look again at:

# ps -ef|awk '/cron/ && !~/awk/ {print $NF}'

This looks for a match to 'cron' only if there isn't the pattern 'awk' in the same line. Using the original snippet I offered, we amend it to be:

# RETURN=$(ps -ef|awk '/pmon_PO/ && !~/awk/ {print $NF}')

...no 'grep' needed and a process and some I/O saved. Thing green :-)

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: grep colums

> #ps -ef|grep pmon

Very dangerous. Never mix grep and ps! It isn't necessary and when you do, you get nasty results:

ps -ef | grep sh

But I was looking for the sh processes and got bash and ksh and unhashdaemon and even sherri, my receptionist!

Seriously, grep cannot be made to stay inside a field (column is so punch-cardy). While awk can extract the last field, what if the process you want is part of the command line, like grep ora_pmon_PO?

This construct will give you an EXACT match for all processes with the name ora_pmon_PO:

UNIX95=1 ps -f -C ora_pmon_PO

also:

UNIX95=1 ps -f -C sh

Notice: no grep. NOTE: UNIX95=1 is required to enable additional options such as -C, -H and -o. The -o option is really useful to customize your ps command:

UNIX95=1 ps -C ora_pmon_PO -o pid,vsz,args

The man page also tells you that you never grep for pid -- just use the -p option. Check out -H too.

Help stamp out ps|grep in HP-UX...


Bill Hassell, sysadmin