Killing with awk and grep

Joe Smith jes at mbio.med.upenn.edu
Tue Aug 15 23:31:37 AEST 1989


> Why not let awk do all the pattern processing:
> ps -aux | awk '$10 ~ /^-sleeper$/{print "kill -9 " $2}' | sh

A good idea, but you will get burned doing this (I know, I have
scorches to prove it!).  Ps is not particularly nice about making it's
output easy to parse.  Some fields have spaces; and, under some
circumstances, others may run together (i.e., no space in between).
Also I think ps output differs among different Unix flavors (I'm only
familiar with BSD).  All this means that using the default awk fields
is a Bad Thing.

If you want to use awk, just scan the whole line:

/-sleeper/ && !/awk/ { print $2 }	# field 2 (pid) is (always?) ok

If you want to look at fields, you'll have to use the substring
function (only in new awk) to break up the lines, but beware, it won't
be portable.

Not that this topic is worth a lot of discussion, but I'd hate to see
someone else get burned on this.

<Joe
--
 Joe Smith
 University of Pennsylvania                    jes at mbio.med.upenn.edu
 Dept. of Biochemistry and Biophysics          (215) 898-8348
 Philadelphia, PA 19104-6059



More information about the Comp.unix.questions mailing list