free | grep Mem | awk '{print $4/$2 * 100.0}'
I want to remove the decimal places from this output.
CodePudding user response:
A couple observations:
- No need for using
grep
asawk
can handle that - Use
printf
instead ofprint
to control the precision
free | awk '/Mem/ { percent = $4 / $2 * 100.0; printf "%0.0f\n", percent; }'
CodePudding user response:
- Use awk /pattern/ to select the correct row.
- Convert to integer to get rid of decimal points.
Code:
free | awk '/Mem/ { print int(100 * $4 / $2) }'
CodePudding user response:
free | grep Mem | awk '{print $4/$2 * 100.0}' | cut -d. -f1
or
free | grep Mem | awk '{print int($4/$2 * 100.0)}'
CodePudding user response:
You can convert to int in awk
free | awk '/^Mem/ { print int($4 * 100.0/$2) }'
12