21

I have the following ps command to get particular properties of all the running processes along with some properties:

ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args"

I wish to have it formatted in CSV so I can parse it. Note I have put the args at the end to make parsing easy; I don't think a , will exist in any of the other columns - please correct me if I am wrong.

How do I remove the whitespace?

1

3 Answers 3

18

From the man page:

-o format       user-defined format.
                format is a single argument in the form of a blank-separated or comma-separated list, which offers a
                way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
                SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
                desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
                output. Column width will increase as needed for wide headers; this may be used to widen up columns
                such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
                (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
                output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
                when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
                DefBSD are macros that may be used to choose the default UNIX or BSD columns.

So try:

/bin/ps -o uname:1,ppid:1,pid:1
3
  • Would you mind selecting this as the Accepted Answer? @jeff-schaller Commented Jun 30, 2019 at 23:40
  • This doesn't output CSV. See stackoverflow.com/a/54857780/128977 for CSV output directly from ps. Commented Jul 2, 2021 at 17:01
  • Where eactly does this work @Fel Commented Jul 23, 2021 at 13:52
6

Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:

ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
  s/[\"]/\\&/g
  s/  */,/;s/  */,/;s/  */,/;s/  */,/;s/  */,/;s/  */,"/
  s/$/"/'

Here quoting the last field (args) after having escaped the "s and \s with \.

Produces an output like:

stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo\"bar"
2

You can use sed with ps. So what you want is here:-

ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/\ /,/g'

But I wonder if it would be useful, as output of ps itself has got lots of ,.

3
  • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want. Commented Sep 1, 2014 at 13:19
  • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column. Commented Sep 1, 2014 at 13:19
  • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here. Commented Sep 1, 2014 at 13:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.