6

I'd like to specify the column-separator

ps -o "%a|%p"        # separator |

and the column-width

ps -o cmd:50,pid     # width 50 for cmd

in one command. Is this possible??

It is not about the column-width but I'd like to have the full length command even if it is not the last column.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
chris01
  • 571

2 Answers2

4

I need the FULL command and the PID. The command should be 1st.

As you need to adjust the output for just 2 columns in predefined order (cmd and pid)
here's ps + sed "trick":

ps -ao "cmd:50,pid" | sed 's/./&|/51'

Test output:

CMD                                                |  PID
nano jq1 -c                                        | 6041
man ps                                             |13714
pager                                              |13725
ps -ao cmd:50,pid                                  |13950
sed s/./&|/51                                      |13951
python                                             |15345
bash -v                                            |16513
bash +v                                            |16645
top                                                |22426
awk -F| -v cmd=xmlstarlet ed -L -u "//Add/value[%d |31412
sh -c xmlstarlet ed -L -u "//Add/value[1]/@IP" -v  |31413
xmlstarlet ed -L -u //Add/value[1]/@IP -v 1.1.1.1  |31414
4

The -o command is additive, so just do multiple of those:

ps -a -o "cmd:50 " -o "|%p"
CMD                                               |  PID
/usr/lib/gnome-session/gnome-session-binary --auto| 4158

The problem is that you'll get some whitespace around the PID as its right-justified. You can see the 4158 above has a space after the pipe.