How to format output of ps -p command?
To not show me something like this:
PID TTY TIME CMD
but just PIDs.
I'm using Linux.
Use the -o option to select which columns are displayed. If you put = after the column name, the header line is suppressed.
ps -o pid= -p 1 23 456
ps -o pid= -o ppid= -o pgid= -o sid= -p 1 23 456
Use the --no-headers to have the header line omitted.
Original output:
ps
PID TTY TIME CMD
27027 pts/0 00:00:00 bash
27071 pts/0 00:00:00 ps
With --no-headers:
ps --no-headers
27027 pts/0 00:00:00 bash
27072 pts/0 00:00:00 ps
Combining with -p:
ps -p 1 --no-headers
1 ? 00:00:33 systemd
The man page for ps clearly documents this:
man ps
[snip]
--no-headers
Print no header line at all. --no-heading is an alias for this option.
= after the column name to the o flag is a cross-platform way to do this. E.g., ps -p ${pid} -o state= will show the process state for ${pid}, without the header. (Which is what the accepted answer states :) )
To get a list of only the PIDs use:
ps -eo pid
See also man ps section 'STANDARD FORMAT SPECIFIERS' for possible colums.
Personally I like the way Solaris shows the possible columns to use for the -o argument:
ps: option requires an argument -- o
usage: ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]
[ -u userlist ] [ -U userlist ] [ -G grouplist ]
[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] [-h lgrplist]
'format' is one or more of:
user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
f s c lwp nlwp psr tty addr wchan fname comm args projid project pset lgrp