如何修改以下 ps 命令以以逗号分隔值 (CSV) 格式打印?
How can I modify following ps command to print in comma separated value (CSV) format?
我有以下命令:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | sed '5q' | awk ' {print ,,, }'
它的输出是:
%CPU %MEM COMMAND PID
0.0 0.1 /sbin/init 1
0.0 0.0 [kthreadd] 2
0.0 0.0 [ksoftirqd/0] 3
0.0 0.0 [kworker/0:0H] 5
我如何修改上面给出的原始命令以 csv 格式打印出来,如下所示:
%CPU, %MEM, COMMAND, PID
0.0, 0.1, /sbin/init, 1
0.0, 0.0, [kthreadd], 2
0.0, 0.0, [ksoftirqd/0], 3
0.0, 0.0, [kworker/0:0H], 5
使用输出字段分隔符 (OFS)。将 OFS=", ";
添加到您的 awk 命令:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | sed '5q' | awk ' {OFS=", "; print ,,, }'
或更短:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | awk 'NR<=5 {OFS=", "; print ,,, }'
输出(示例):
%CPU, %MEM, COMMAND, PID
0.0, 0.0, [migration/0], 6
0.0, 0.0, [migration/1], 7
0.0, 0.0, [migration/2], 11
0.0, 0.0, [migration/3], 14
参见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
我有以下命令:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | sed '5q' | awk ' {print ,,, }'
它的输出是:
%CPU %MEM COMMAND PID
0.0 0.1 /sbin/init 1
0.0 0.0 [kthreadd] 2
0.0 0.0 [ksoftirqd/0] 3
0.0 0.0 [kworker/0:0H] 5
我如何修改上面给出的原始命令以 csv 格式打印出来,如下所示:
%CPU, %MEM, COMMAND, PID
0.0, 0.1, /sbin/init, 1
0.0, 0.0, [kthreadd], 2
0.0, 0.0, [ksoftirqd/0], 3
0.0, 0.0, [kworker/0:0H], 5
使用输出字段分隔符 (OFS)。将 OFS=", ";
添加到您的 awk 命令:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | sed '5q' | awk ' {OFS=", "; print ,,, }'
或更短:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | awk 'NR<=5 {OFS=", "; print ,,, }'
输出(示例):
%CPU, %MEM, COMMAND, PID 0.0, 0.0, [migration/0], 6 0.0, 0.0, [migration/1], 7 0.0, 0.0, [migration/2], 11 0.0, 0.0, [migration/3], 14
参见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR