Could you help me to modify the following one-liner to get more precise memory usage - right now it's just an integer I would like to include first value after coma.

free -m | awk '/Mem:/ {tot = $2;} /cache:/{printf "%d\n", $3 / tot * 100}'

Thank you

  • 4
    printf "%.1f\n" ? – ripat Jun 6 '12 at 19:33

Your %d format specifier is forcing printf to print an integer value. If you change it to %f then it would print the full number. You can use %.nf to limit the number of decimal places printed so %.1f would print one decimal place.

  • Already posted above, although with less explanations ^_^ – ripat Jun 6 '12 at 19:49
  • 3
    @ripat Should have made it an answer :). If the correct answer is in a comment, then it's fair game to be "reposted" as an answer, since an OP cannot mark a comment as "accepted". – MDMarra Jun 6 '12 at 19:56
  • @MDMarra. I agree. My apologies but the answer was so short and obvious... – ripat Jun 7 '12 at 14:49
  • @ripat: obvious to you and I because we know the answer but clearly not obvious to everyone. As you can see, answers don't have to be short either, I think longer is better. – Iain Jun 7 '12 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.