I have a script to calculate the cumulative capacity in all the servers.

for i in `cat servers`
ssh user@$i "df -t xfs --total | grep total";
done > capout.txt
awk -vDT="Capacity" '
        {sum2 += $2
         sum3 += $3
         sum4 += $4
         sum5 += $5
END     {GIGA=2^30
          print DT,"\t" sum2/GIGA,"\t" sum3/GIGA,"\t" sum4/GIGA ,"\t"  sum3/sum2*100
        }' capout.txt >> capacity.csv

Script works fine and i am getting the output as below.

Capacity   1047.43         439.4   608.032         41.9502

However i do not want to have the 3 trailing decimal points, I just need to restrict to one or two decimal points here.

  • THe answer to your question would be to use printf("%d\n", sum3/GIGA....) formatting. The script as written has bugs and should be recosidered: Also see mywiki.wooledge.org/BashFAQ/094 and mywiki.wooledge.org/BashFAQ/089 – Valentin Bajrami Jan 30 '19 at 9:56
  • 2
    Try adding a -v OFMT=%.2f – Stéphane Chazelas Jan 30 '19 at 10:25
  • @ValentinBajrami I don't think i have got you completely. But i have tried below and seems it is not giving me any output.... printf(DT,"%d\t" sum2/GIGA,"%d\t" sum3/GIGA,"%d\t" sum4/GIGA ,"%d\t" sum3/sum2*100) – xrkr Jan 30 '19 at 11:16
  • @xrkr printf doesn't work that way. You first define the formatting than place the variables i.e printf("%s %d\t%d\t", DT,sum2/GIGA,sum3/GIGA)}' and so on. But I think you should also follow Stephanes' advise. Set the Output format to %.2f. This means you'll only get 2 digits after the dot . – Valentin Bajrami Jan 30 '19 at 11:21
  • @ValentinBajrami Stéphane Chazelas Thanks it worked. – xrkr Jan 30 '19 at 12:20

Browse other questions tagged or ask your own question.