尝试使用AWK计算使用的内存%

时间:2017-05-18 09:15:17

标签: memory awk

我尝试使用 AWK 来计算已用内存 % from vmstat -s。以下是我试过的命令

vmstat -s | awk 'NR==1 {total=$0}; NR==2 {used=$0}; END {print usedpct=used/total*100}'

但我似乎无法获得我想要的输出。

任何建议表示赞赏。 谢谢。

2 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

bash-4.2$ cat file1
D,"4/2/2017 2:45:56 PM",ee,"4/2/2017 2:45:56 PM"
D,"03/02/2017 03:47:16 PM",ee,"03/02/2017 03:47:16 PM"
D,"09/2/2017 6:05:54 AM",ee,"09/2/2017 6:05:54 AM"
D,"5/01/2017 8:29:46 PM",ee,"5/01/2017 8:29:46 PM"
D,"4/2/2017 02:3:26 AM",ee,"4/2/2017 02:3:26 AM"

bash-4.2$ sed -i 's|\b\([[:digit:]]\)\b|0\1|g' file1

bash-4.2$ cat file1
D,"04/02/2017 02:45:56 PM",ee,"04/02/2017 02:45:56 PM"
D,"03/02/2017 03:47:16 PM",ee,"03/02/2017 03:47:16 PM"
D,"09/02/2017 06:05:54 AM",ee,"09/02/2017 06:05:54 AM"
D,"05/01/2017 08:29:46 PM",ee,"05/01/2017 08:29:46 PM"
D,"04/02/2017 02:03:26 AM",ee,"04/02/2017 02:03:26 AM"

答案 1 :(得分:1)

另一个 awk 方法:

vmstat -s | awk 'NR>2{exit}{a[NR]=$1}END{print a[2]/a[1]*100}'

NR>2{exit} - 仅考虑前两行