Simple awk question

Here is an awk line I have in a bigger script that checks to see if nimsh process is running and does couple other things based on the output and runs on all servers.

ps -ef|grep -i nimsh|awk '{print $9}'    and I am expecting output to be "/usr/sbin/nimsh"

I find that on some servers because of discrepancy in the 5th field(may in some servers and timestamp in others) , my nimsh becomes 8th or 9th field.

How can i write a statement so that it yields /usr/sbin/nimsh on all servers?

server A:

root  7274540  6684890   0 14:35:25      -  0:00 /usr/sbin/nimsh -s

server B:

    root  8061204  4849926   0   May 31      -  0:00 /usr/sbin/nimsh -s

Try using the NF operator, considering the input.

yourcode .... | awk ' { print $(NF-1) } '

Hope that helps
Regards
Peasant.

If the grep should match the nimsh string in $(NF-1) then consider replacing the whole-line grep by the more precise

ps -fu root | awk '$(NF-1) ~ /nimsh/ { print $(NF-1) }'

Unlike the catch-all ps -fe the ps -fu root filters for user root.
Even smarter is

ps -fu root -o args= | grep '[n]imsh'

Note the [n] trick that makes grep not find its own args in the process table.

You could be more precise in the usage of the `ps` command. Instead of using

ps -ef

(as everybody does) you might specify:

ps -e -o pid,comm,command

This would give you the pid, the short command and the long command.

Your awk question seems to have been fully and satisfactorily answered, but your task is a different one. Did you ever consider

pgrep -a nimsh

to test for a running process?