8

I know that

ps ax

returns the pids

1 ?        Ss     0:01 /sbin/init
2 ?        S<     0:00 [kthreadd]
3 ?        S<     0:00 [migration/0]

All I need is to clean those strings, but I couldn't do it with sed because I couldn't write the proper regex. Could you help me?

Jader Dias
  • 4,625
  • 18
  • 48
  • 50

4 Answers4

19

Use ps Output Formatting:

ps -A -o pid

Output formatting of the command is the best option. The o option controls the output formatting. I listed some of arguments below below, see 'man ps' for the rest ( to use multiple it would be -o pid,cmd,flags).

KEY   LONG         DESCRIPTION
   c     cmd          simple name of executable
   C     pcpu         cpu utilization
   f     flags        flags as in long format F field
   g     pgrp         process group ID
   G     tpgid        controlling tty process group ID
   j     cutime       cumulative user time
   J     cstime       cumulative system time
   k     utime        user time
   o     session      session ID
   p     pid          process ID

Awk or Cut Would be Better to get Columns:
Generally you wouldn't want a regex for selecting the first column, you would want to pipe it to cut or awk to cut out the first column like:

ps ax | awk '{print $1}'

Regex is an Option, if not the best:
If you were to use regex, it could be something like:

ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'

$1 prints only what was matched in the parenthesis. ^ anchors the to the start of the line. Space asterisk means allow for optional space characters before the number. [0-9]+ means one or more digits. But I wouldn't recommend regex for this particular task, see why? :-)

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
  • thanks! but the regular expression would be a great addition, if anyone could contribute with a second answer – Jader Dias Aug 12 '09 at 18:33
  • 1
    really, use awk. – David Pashley Aug 12 '09 at 18:42
  • Pay attention, the regex would not match the initial spaces eventually being before small pids. Moreover a pid should be make of a digit at least, not zero (you should use + and not *) – drAlberT Aug 12 '09 at 18:47
  • AlberT, oh good point, Didn't see that they were right justified. That is why I said regex is not the best I guess, will fix :-) – Kyle Brandt Aug 12 '09 at 18:50
  • I can't argue how to use cut, by means of the initial spaces of course. Can you point out to me how to use cut? Just for the sake of curiosity :) – drAlberT Aug 12 '09 at 19:06
  • Ya, guess cut doesn't work for this particular example... But I was just make a generalization about selecting columns. I don't really like cut though, I like awk / perl these days. – Kyle Brandt Aug 12 '09 at 19:44
  • AlberT: But Actually, it can be done very elegantly with cut, see;-) num=$(ps ax | cut -f1 -d' ' | tail -1 | wc -c); ps ax | cut -c1-$num – Kyle Brandt Aug 12 '09 at 19:54
6
ps ax | awk '{ print $1; }'
Chad Huneycutt
  • 2,096
  • 1
  • 16
  • 14
5

Use the -o switch to have a cust format output

ps -o pid

The bad way using sed, as you explicitly asked may be

ps -ax | sed 's#^\( *[0-9]\+\) .*$#\1#'
drAlberT
  • 10,871
  • 7
  • 38
  • 52
0

ps -eo pid or ps -eo %p

-e Select all process -o Format pid=process id %p sameas pid