free | grep Mem | awk '{print $4/$2 * 100.0}'
I want to remove the decimal places from this output.
free | grep Mem | awk '{print $4/$2 * 100.0}' | cut -d. -f1
or
free | grep Mem | awk '{print int($4/$2 * 100.0)}'
A couple observations:
grep
as awk
can handle thatprintf
instead of print
to control the precisionfree | awk '/Mem/ { percent = $4 / $2 * 100.0; printf "%0.0f\n", percent; }'
Code:
free | awk '/Mem/ { print int(100 * $4 / $2) }'
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.