[plug] help with sed

Russell Steicke r.steicke at bom.gov.au
Thu Mar 15 12:18:23 WST 2001


Hi,

I'm having a spot of trouble with a sed regex.  I want to do a ps
listing and extract the pid of a certain set of processes that match an
expression (using grep) and then print just their pids.  Now, awk will
do that.  "ps x | grep foo | awk '{print $1}'" works fine, but I thought
it would be fun to use sed instead.

So I tried

  ps x |grep foo | sed 's/^\ *([0-9]+).*$/\1/g'

which prints the whole lines, not just the pids.  All the variations I
tried (using '\(...\)' instead of '(...)', not escaping the space, not
substituting the replacement \1, using a character other than '/' around
the regex, etc) all printed the whole line.

The only thing I could do that had much effect was

  ps x |grep foo |sed 's=^\ *[0-9]*==g'

which strips out the pid.  Not quite what I want, but at least it did
something.  Even

  ps x |grep foo |sed 's=^\ *[0-9]+==g'

prints the whole line, including the pid which (I believe) should be
matched by '^\ *[0-9]+'. (Note the second * replaced by a +).

There is apparently something fundamental about sed's regex that I've
missed.  My reading of the gawk info page (which is where the sed info
refers me in the regex section) suggests that the first one should work.
This is GNU sed 3.02 on debian.  Can anyone enlighten me about what I've
missed?



-- 
Russell Steicke

-- Fortune says:
Men have a much better time of it than women; for one thing they marry later;
for another thing they die earlier.
		-- H.L. Mencken



More information about the plug mailing list