3

My current script is giving output with many decimal places. Is there any way to get only 1 place in the output?

# cat sample
HDD Used: 15.0223T
HDD Total: 55.9520T

# cat sample | awk ' /HDD Total/ { hdd_total=$NF }
      /HDD Used/ { hdd_used=$NF }
      END {
        used=hdd_total-hdd_used
        print "cal =" used}'

Current Output

cal =40.9297

Required output
cal =40.9 ---> With only one decimal

Getting error for this

# isi storagepool list -v| grep -i 'HDD Total:' | awk '{print "HDD Total=%.1f", $NF -1 " TB" }'
HDD Total=%.1f 54.952 TB
#cat isistorage1 
7.332T     n/a (R)   13.01% (T)

# cat isistorage1 |  awk '{ print "Snapshot USED=", $1}'
Snapshot USED= 7.332T
Shanker
  • 117

2 Answers2

10

You can use printf "cal =%.1f\n", used instead since it has print type modifier control and area.

The .1f there means, only print 1 decimal place after a floating point, you can change it to any number you want in decimal places.

Archemar
  • 31,554
αғsнιη
  • 41,407
3

You can tell awk which floating-point conversion to use by setting CONVFMT, the default is %.6g, e.g.:

cat sample | awk -v CONVFMT='%.1f' '
/HDD Total/ { hdd_total = $NF }
/HDD Used/  { hdd_used  = $NF }
END {
  used=hdd_total-hdd_used
  print "cal = " used
}'

Output:

cal = 40.9
Thor
  • 17,182