Grep CPU 和其他硬件百分比使用 top 命令?

Grep CPU and other hardware percentage using top command?

我需要测量我的 CPU 使用百分比和内存。
使用 top 命令会给出这个结果(见红色标志)

输入另一个命令

top -d 1 | grep -w "CPU"

结果是这样的。

如何将 cpu 的使用百分比值获取到 bash linux 中的变量?
需要大家帮助:)
非常感谢

要获取 CPU 百分比值,您可以使用 top -n1 -b 命令并将其通过管道传输到 awk:

top -n 1 -b | awk '/^%Cpu/{print }'

我认为使用 mpstat 而不是 top 更容易解析有关处理器的信息。

mpstat -P ALL 1 1

-P to indicate processor `ALL` for all of them
and shows 1 interval of 1 second