我试图从命令的输出中提取 %user、%nice 等的值sar
。
sar -P ALL 1 1
输出:
Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com) 09/28/2015 _x86_64_ (4 CPU)
02:49:40 PM CPU %user %nice %system %iowait %steal %idle
02:49:41 PM all 3.01 0.00 2.51 0.00 0.00 94.49
02:49:41 PM 0 1.98 0.00 3.96 0.00 0.00 94.06
02:49:41 PM 1 6.00 0.00 4.00 0.00 0.00 90.00
02:49:41 PM 2 2.00 0.00 1.00 0.00 0.00 97.00
02:49:41 PM 3 1.98 0.00 2.97 0.00 0.00 95.05
Average: CPU %user %nice %system %iowait %steal %idle
Average: all 3.01 0.00 2.51 0.00 0.00 94.49
Average: 0 1.98 0.00 3.96 0.00 0.00 94.06
Average: 1 6.00 0.00 4.00 0.00 0.00 90.00
Average: 2 2.00 0.00 1.00 0.00 0.00 97.00
Average: 3 1.98 0.00 2.97 0.00 0.00 95.05
我已经编写了以下字符串来获取值,但这似乎不起作用。
我的命令:
sar -P ALL 1 1 | \
awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}' \
'{printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, \
pctIowait, pctIdle}'
答案1
尝试
sar -P ALL 1 1 |
awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'
NF == 9
您需要过滤九个字段(NF)$3 != "all"
跳过总结 cpu 的行- 没有字符但行尾之后
|
更紧凑的版本
sar -P ALL 1 1 |
awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9s\n",$3,$4,$5,$6,$7,$NF}'
更紧凑的版本
sar -P ALL 1 1 |
awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'