Answers:
从手册页:
-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.
因此,请尝试:
/bin/ps -o uname:1,ppid:1,pid:1
由于前6个字段不应包含空格字符(除非您在用户名中允许使用空格),因此可以对输出进行后处理:
ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
s/[\"]/\\&/g
s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
s/$/"/'
在此引用用逃逸"
s和\
s 后的最后一个字段(args)\
。
产生如下输出:
stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo\"bar"
您可以使用sed
与ps
。所以您想要的是这里:-
ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/\ /,/g'
但是我不知道它是否会有用,因为ps
它本身的输出有很多,
。
,
放在结尾的列,但是,此解决方案,
将从该列中删除我不想要的列。
tr ' ' ,
。您可能想要tr -s ' ' ,
在这里。