Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk: search last column which ends with a string 2

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
ps -fu|...

produces lines which can have last column like:

./abcd.sh
abcd.sh
/home/user/abcd.sh
./user/abcd.sh

I need to find all lines which either alone "abcd.sh" or "/abcd.sh" and also to be sure is not preceded by commands like vi or more (only running script processes).

What I have combined now is:

Code:
# P=abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^vi -e ^more

how to get exactly what I need?
how to print columns which ends with "/abcd.sh" or are alone "abcd.sh" ($NF == a)?
 
Hi

Are you sure you need all columns printed by [tt]ps[/tt] ? If not, better ask it to print only the command itself :
Code:
ps -uroot -ocomm | grep 'abcd.sh'
( On Linux you could use [tt]pgrep[/tt] from the procps package. But as far as I know, Unixes not have it. )


Feherke.
 
hello,

simple grep doesn't catch what we want:

Code:
# ps -ef|grep abcd
    root 12517398  7798976   1 15:14:20  pts/2  0:00 grep abcd
    root 17956998 12779584   0 15:13:32  pts/1  0:00 /usr/bin/ksh ./abcd.sh
# ps -uroot -ocomm | grep 'abcd.sh'
#


I've combined such command (but don't belive we can't do it easier...)

Code:
# N=abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^more -e ^vi|awk '{print $NF}'|while read a;do echo ${a##*/};done|grep -c ^${N}$
1
#


also here I don;t know why "grep -w" returns matched if the string does not match (whole word I guess should match):

Code:
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'
/usr/bin/ksh ./abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^more -e ^vi
/usr/bin/ksh ./abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^more -e ^vi|grep -w [/]${N}$
/usr/bin/ksh ./abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^more -e ^vi|grep -w ${N}$
/usr/bin/ksh ./abcd.sh
# ps -furoot|awk -va=$N '$NF ~ a {print $(NF-1)" "$NF}'|grep -v -e ^more -e ^vi|awk '{print $NF}'|grep -w ${N}$
./abcd.sh
# echo "./abcd.sh"
./abcd.sh
# echo "./abcd.sh"|grep -w [/]${N}$
./abcd.sh
# echo [COLOR=red]"./abcd.sh"[/color]|grep [COLOR=red]-w[/color] ${N}$
[COLOR=red]./abcd.sh[/color]
# echo $N
[COLOR=red]abcd.sh[/color]
#
 
If you also want to search the parameters in a command line rather than the command name alone, use -o args instead of -o comm.

Are you saying that you would expect echo "./abcd.sh"|grep -w ${N}$ not to match? If so, you misunderstand what -w tries to do; it ignores non-word characters such as "./" when matching to try and match "whole words".

I assume abcd.sh has a #!/usr/bin/ksh shebang line, in which case it should always appear in ps as /usr/bin/ksh ... abcd.ksh, so that is what I would search for.

Code:
ps -e -o pid= -o args= | grep '/usr/bin/ksh .*abcd.sh'

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
thank you Annihilannic,

can I tell to the "grep" to not output it's own command or I need to use another grep (| grep -v grep)?
 
tell to the "grep" to not output it's own command
One way:
Code:
ps -e -o pid= -o args= | grep '/usr/bin/ksh[!][ ][/!].*abcd.sh'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thank you.
please tell me how the "[ ]" is interpreted by grep

becasue I recognized that all below examples seems to do the same (eliminates grep's process from ps output):

Code:
# echo "/usr/bin/ksh ./jhd/dsfjh/abcd.sh" | grep '/usr/bin/ksh[COLOR=red][ ][/color red].*abcd.sh'
/usr/bin/ksh ./jhd/dsfjh/abcd.sh
# echo "/usr/bin/ksh ./jhd/dsfjh/abcd.sh" | grep '/usr/bin/ksh[COLOR=red][ ][/color red].*abcd.s[COLOR=red][h][/color red]'
/usr/bin/ksh ./jhd/dsfjh/abcd.sh
# echo "/usr/bin/ksh ./jhd/dsfjh/abcd.sh" | grep '/usr/bin/ksh[COLOR=red][ ][/color red].*ab[COLOR=red][c][/color red]d.s[COLOR=red][h][/color red]'
/usr/bin/ksh ./jhd/dsfjh/abcd.sh
# echo "/usr/bin/ksh ./jhd/dsfjh/abcd.sh" | grep '/usr/bin/ksh .*abcd.s[COLOR=red][h][/color red]'
/usr/bin/ksh ./jhd/dsfjh/abcd.sh
#
 

I still have one issue:

Code:
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*abcd.sh'
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
[COLOR=red]/usr/bin/ksh ./abcd.shsh[/color red]
[COLOR=red]/usr/bin/ksh /root/zxyabcd.sh[/color red]
[COLOR=red]/usr/bin/ksh ./zxyabcd.sh[/color red]
#

How to get rid off the the marked in red so get only script filename "abcd.sh" ?

I can eliminate file ended .shsh

Code:
# ps -uroot -e -o args=|grep abc[d]|sed s/$/_end/g
/usr/bin/ksh /root/abcd.sh _end
/usr/bin/ksh ./abcd.sh _end
/usr/bin/ksh ./abcd.shsh _end
/usr/bin/ksh /root/zxyabcd.sh _end
/usr/bin/ksh ./zxyabcd.sh _end
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*abcd.sh '$
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
/usr/bin/ksh /root/zxyabcd.sh
/usr/bin/ksh ./zxyabcd.sh
#

but how to get rid off the others?
 

Ok, I assumed the ps output will never return:

/usr/bin/ksh abcd.sh

Am I right?

If yes, this solves my last issue:

Code:
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*/abcd.sh '$
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
#
 
I'd use this:
Code:
ps -uroot -e -o args= | grep '^/usr/bin/ksh[ ].*abcd.sh *$'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
hello PVH,

your proposal returns too much:

Code:
# ps -uroot -e -o args= | grep '^/usr/bin/ksh[ ].*abcd.sh *$'
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
/usr/bin/ksh /root/zxyabcd.sh
/usr/bin/ksh ./zxyabcd.sh
#

but modifying a bit returns what is expected:
Code:
# ps -uroot -e -o args= | grep '^/usr/bin/ksh[ ].*[COLOR=red]/[/color red]abcd.sh *$'
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
#


Please tell me if below is correct in case variable is used:

Code:
# P=abcd.sh
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*/$P '$
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*/${P} '$
# ps -uroot -e -o args=|grep ^'/usr/bin/ksh[ ].*/'${P}' '$
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
# ps -uroot -e -o args=|grep ^[b][COLOR=blue]'[/color blue][/b]/usr/bin/ksh[ ].*/[b][COLOR=blue]'[/color blue][/b]$P[b][COLOR=red]'[/color red][/b] [b][COLOR=red]'[/color red][/b]$
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
# ps -uroot -e -o args=|grep ^[b][COLOR=blue]'[/color blue][/b]/usr/bin/ksh[ ].*/[b][COLOR=blue]'[/color blue][/b]$P[b][COLOR=red]'[/color red][/b] *$[b][COLOR=red]'[/color red][/b]
/usr/bin/ksh /root/abcd.sh
/usr/bin/ksh ./abcd.sh
#


 
Oops, sorry for the the type (lack of /)
As for the variable P:
Code:
ps -uroot -e -o args= | grep "^/usr/bin/ksh[ ].*/$P *$"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
w5000 said:
please tell me how the "[ ]" is interpreted by grep

[ ] in a regular expression matches a range of characters, but in this case we are specifying only one. Normally this would make no sense, but when filtering a process listing it helps because the grep command appears as grep "something[ ]else", which does not match the regular expression something[ ]else. So it will only match the string which you we were actually looking for, in this case, "something else".

As you found it doesn't matter which single character you use with this trick, just the presence of the [ and ] characters is enough.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top