Home > OS >  How can I make this bash code remove the decimal places?
How can I make this bash code remove the decimal places?

Time:04-14

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:

  1. No need for using grep as awk can handle that
  2. Use printf instead of print to control the precision
free | awk '/Mem/ { percent = $4 / $2 * 100.0; printf "%0.0f\n", percent; }'

CodePudding user response:

  1. Use awk /pattern/ to select the correct row.
  2. 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
  • Related