bash Linux top -b 仅显示特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19816412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux top -b show specific columns only
提问by axujen
I would like to capture the output of the top command to use in another program however I only need certain information, more precisely I only need the USER, PID, CPU, COMMAND columns.
I already have the command top -b -n 1 | sed -n '7,12p'
to filter the top 5 results but I cannot go any further because I do not know much about sed/awk.
我想捕获要在另一个程序中使用的 top 命令的输出,但是我只需要某些信息,更准确地说,我只需要 USER、PID、CPU、COMMAND 列。我已经有了top -b -n 1 | sed -n '7,12p'
过滤前 5 个结果的命令,但我不能再进一步了,因为我对 sed/awk 不太了解。
Example: here is what I get
示例:这是我得到的
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4 root 20 0 98748 50608 4608 S 6.4 4.9 212:12.16 X
1 root 20 0 2132 128 96 S 0.0 0.0 0:07.62 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 7:28.54 ksoftirqd/0
and here is what i want
这是我想要的
PID USER %CPU COMMAND
4 root 6.4 X
1 root 0.0 init
2 root 0.0 kthreadd
3 root 0.0 ksoftirqd/0
采纳答案by Kent
pass to:
传递给:
awk '{print ,,,$NF}'
回答by Jotne
Everything combined.
一切结合在一起。
top -b -n 1 | awk 'NR>6 && NR<13 {printf "%6s %-4s %-4s %-s\n",,,,$NF}'