使用 top 从 PID 和 COMMAND 获取前 5 行
Getting the first 5 lines from PID and COMMAND using top
嗨,我正在努力弄清楚如何在打开 top
时获取 PID 和 COMMAND headers 的前 5 行
目前我正在使用
top | awk '{print ,}NR==5{exit}'
这显然让我恼火:
Processes: 390
2021/12/17 13:47:48
Load Avg:
CPU usage:
SharedLibs: 146M
使用您显示的示例,请尝试以下代码。由于您需要前 5 个 PID 详细信息,因此打印前 5 行在这里不起作用。所以跳过 top 输出的前 6 行(关于系统详细信息)。
top -b | awk 'FNR>=7 && FNR<=12{print };FNR==12{exit}'
解释: 上面代码的简单解释是,将 top
命令的输出传递给 awk
作为标准输入。然后在 awk
程序中检查条件,如果行号是从第 7 行到第 12 行,然后打印它并在第 12 行退出程序。
其中 top -b
选项的定义如下来自 man top
:
b :Batch-mode operation Starts top in Batch mode, which could be
useful for sending output from top to other programs or to a file. In
this mode, top will not accept input and runs until the iterations
limit you've set with the `-n' command-line option or until killed.
其他解决方案:
top -b 用于批处理模式
top -n 1 单次拍摄(无循环)
获取行数
- 用 --> 头 -12 |尾巴-6
- 或 --> awk 'FNR>=7 && FNR<=12'
- 或 --> sed -n 7,12p
打印 PID 和命令
- awk '{打印 $1, $NF}'
...
top -b|head -12|tail -6|awk '{print , $NF}'
top -b -n 1|awk 'FNR>=7 && FNR<=12{print , $NF}'
top -b -n 1|sed -n 7,12p|awk '{print , $NF}'
对于MAC OS X,命令为:
top -a -n20 | awk 'FNR>=11 && FNR<=31{print [=10=]};FNR==31{exit}' > cpustat.txt
嗨,我正在努力弄清楚如何在打开 top
时获取 PID 和 COMMAND headers 的前 5 行目前我正在使用
top | awk '{print ,}NR==5{exit}'
这显然让我恼火:
Processes: 390
2021/12/17 13:47:48
Load Avg:
CPU usage:
SharedLibs: 146M
使用您显示的示例,请尝试以下代码。由于您需要前 5 个 PID 详细信息,因此打印前 5 行在这里不起作用。所以跳过 top 输出的前 6 行(关于系统详细信息)。
top -b | awk 'FNR>=7 && FNR<=12{print };FNR==12{exit}'
解释: 上面代码的简单解释是,将 top
命令的输出传递给 awk
作为标准输入。然后在 awk
程序中检查条件,如果行号是从第 7 行到第 12 行,然后打印它并在第 12 行退出程序。
其中 top -b
选项的定义如下来自 man top
:
b :Batch-mode operation Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the `-n' command-line option or until killed.
其他解决方案:
top -b 用于批处理模式
top -n 1 单次拍摄(无循环)
获取行数
- 用 --> 头 -12 |尾巴-6
- 或 --> awk 'FNR>=7 && FNR<=12'
- 或 --> sed -n 7,12p
打印 PID 和命令
- awk '{打印 $1, $NF}'
...
top -b|head -12|tail -6|awk '{print , $NF}'
top -b -n 1|awk 'FNR>=7 && FNR<=12{print , $NF}'
top -b -n 1|sed -n 7,12p|awk '{print , $NF}'
对于MAC OS X,命令为:
top -a -n20 | awk 'FNR>=11 && FNR<=31{print [=10=]};FNR==31{exit}' > cpustat.txt