You are not logged in.

#1 2014-05-07 19:07:16

publicus
Member
Registered: 2014-01-01
Posts: 129

[RESOLVED] Having issue with this awk script

Hello,

Here is my little script.

ps -eo comm,pid,args | awk '$1 == "java" { print $2 }'

It works well.  However, in addition to comparing $1 to the name of the process, I'd like to search args and make sure that it does not have certain values (say "efox".)

This is what I've tried so far.

ps -eo comm,pid,args | awk '(($1 == "java") && (!match($3, "amboo"))) { print $2 $1 }'

But that does not give me _any_ PIDs now and there should be 3 (the previous script gives me 4.)

Any ideas?

Last edited by publicus (2014-05-07 19:44:47)

Offline

#2 2014-05-07 19:12:35

publicus
Member
Registered: 2014-01-01
Posts: 129

Re: [RESOLVED] Having issue with this awk script

Tried this, also did not work.

ps -eo comm,pid,args | awk '(($1 == "java") && (index($3, "amboo")) == 0) { print $2 }'
21950
21981
22038
22116

2116 is not supposed to be listed there.

Last edited by publicus (2014-05-07 19:13:14)

Offline

#3 2014-05-07 19:44:35

publicus
Member
Registered: 2014-01-01
Posts: 129

Re: [RESOLVED] Having issue with this awk script

Ok, this is what I needed to do:

$ ps -eo comm,pid,args | awk '(($1 == "java") && ((index($4, "amboo")) == 0)) { print $2 }'

I'm a n00b to awk, but -- correct me if I'm wrong -- the individual pieces of information are delimited by spaces and the args that I was sifting through was something like this "/some/folder/java -Dstuff", the space was throwing me off.

Offline

#4 2014-05-07 21:53:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [RESOLVED] Having issue with this awk script

The default field separator is a space. You can change that using -F.

You could also use something like this (just guessing what your input looks like):

ps -eo comm,pid,args | awk '/java/ && !/amboo/ { print $2 }'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2014-05-09 20:27:53

publicus
Member
Registered: 2014-01-01
Posts: 129

Re: [RESOLVED] Having issue with this awk script

jasonwryan wrote:

The default field separator is a space. You can change that using -F.

You could also use something like this (just guessing what your input looks like):

ps -eo comm,pid,args | awk '/java/ && !/amboo/ { print $2 }'

That code is obsolete now, the project has taken a different turn and I have a solution for that issue.

Thanks for the heads up though.

Offline

Board footer

Powered by FluxBB