简体   繁体   中英

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. However in Mavericks, it returns the error specified below.

$ 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:

$ 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?

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

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

Thank you very much to everyone who contributed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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