简体   繁体   English

如何在OS X Mavericks中使用AWK从顶级-l1获取物理内存?

[英]How to get Physical Memory from top -l1 using AWK in OS X Mavericks?

I'm using this script for GeekTools on mac and the code below worked on previous OSX versions. 我在Mac上使用这个脚本用于GeekTools,下面的代码适用于以前的OSX版本。 However in Mavericks, it returns the error specified below. 但是在Mavericks中,它会返回下面指定的错误。

$ top -l1 | grep "PhysMem"|awk '{print "X"int(($2+$4)/($8+$10)*50)"X"}'

awk: division by zero
 input record number 1, file 
 source line number 1

Output before awk: awk之前的输出:

$ top -l1 | grep "PhysMem"
PhysMem: 6796M used (936M wired), 1282M unused.

As awk is an uncharted territory for me, could someone please post a quick fix for that? 由于awk对我来说是一个未知的领域,有人可以发布快速解决方案吗?

Try these: 试试这些:

top -l 1 | awk '/PhysMem/ {print $4}' | sed s/M// | sed s/\(//

Used memory: 二手记忆:

top -l 1 | awk '/PhysMem/ {print $2}

Free memory: 空闲记忆:

top -l 1 | awk '/PhysMem/ {print $6} 

Cleaning your solution by removing unneeded parentheses and int 通过删除不需要的括号和int清理解决方案

top -l1 | awk '/PhysMem/ {print int((1-$6/($2+$6))*50)}'
42

This achieves the result I was looking for. 这实现了我正在寻找的结果。

top -l1 |awk '/PhysMem/ {print "X"int((1-(int($6)/((int($6)+int($2)))))*50)"X"}'

For anyone's interest and some context, I was trying to convert this CPU geeklet to Memory geeklet: http://freshmacapps.com/geektool-scripts/geektool-cpu-circle-monitor 对于任何人的兴趣和一些上下文,我试图将这个CPU geeklet转换为Memory geeklet: http ://freshmacapps.com/geektool-scripts/geektool-cpu-circle-monitor

Thank you very much to everyone who contributed. 非常感谢所有贡献的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM