0

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?

Lightheaded
  • 644
  • 8
  • 23
  • Do `top -l1 | grep "PhysMem"` and post the output – Fredrik Pihl Jan 16 '14 at 13:10
  • 1
    You do not need `grep` and `awk`, use: `top -l1 | awk '/PhysMem/ {print "X"int(($2+$4)/($8+$10)*50)"X"}'` (does not solve your problem) – Jotne Jan 16 '14 at 13:35
  • I posted the output for `top -l1 | grep "PhysMem"` in the original post, Fredrik. – Lightheaded Jan 16 '14 at 13:48
  • `$8` and `$10` is non existence and give `0`, so dividing on `0` gives error. What do you like to calculate out from your output? `$2=6796M` `$4=(936M` as you see, not clean numbers. – Jotne Jan 16 '14 at 13:52

3 Answers3

1

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} 
l'L'l
  • 44,951
  • 10
  • 95
  • 146
1

Cleaning your solution by removing unneeded parentheses and int

top -l1 | awk '/PhysMem/ {print int((1-$6/($2+$6))*50)}'
42
Jotne
  • 40,548
  • 12
  • 51
  • 55
0

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.

Lightheaded
  • 644
  • 8
  • 23