linux - How to add numbers in a column and insert the total to a differentcolumn -
let me preface saying i'm complete newbie scripting.
we have many mismanaged quota files need totals updated. i'd have script can calculate totals of each volume instead of doing mechanically every month. please see example below.
our quota file looks this:
/vol/vol1/nstorage1 tree 200g /vol/vol1/nstorage2 tree 5g /vol/vol1/nstorage3 tree 300g /vol/vol1/nstorage4 tree 145g /vol/vol1/nstorage5 tree 3g /vol/vol1/nstorage6 tree 5g # # vol1 total 700gb /vol/vol2/nstorage7 tree 20g /vol/vol2/nstorage8 tree 1g /vol/vol2/nstorage9 tree 30g /vol/vol2/nstorage10 tree 55g /vol/vol2/nstorage11 tree 25g /vol/vol2/nstorage12 tree 430g /vol/vol2/nstorage13 tree 20g # # vol2 total 550gb
and on....
what trying take total sum 3rd column, , add "volx total ####gb" line.
i have tried use awk first section of column , don't know how replace total each column.
awk '{ sum += $3 } end { print sum }' < file
any appreciated.
if unit in file gb
, can use line:
awk '!/^#/{s+=$3+0;print;next}nf>1{$nf=$nf+s"gb";s=0}7' file
with input example, output:
/vol/vol1/nstorage1 tree 200g /vol/vol1/nstorage2 tree 5g /vol/vol1/nstorage3 tree 300g /vol/vol1/nstorage4 tree 145g /vol/vol1/nstorage5 tree 3g /vol/vol1/nstorage6 tree 5g # # vol1 total 1358gb /vol/vol2/nstorage7 tree 20g /vol/vol2/nstorage8 tree 1g /vol/vol2/nstorage9 tree 30g /vol/vol2/nstorage10 tree 55g /vol/vol2/nstorage11 tree 25g /vol/vol2/nstorage12 tree 430g /vol/vol2/nstorage13 tree 20g # # vol2 total 1131gb
update
if want sum replace total
value, not add to
, just:
awk '!/^#/{s+=$3+0;print;next}nf>1{$nf=s"gb";s=0}7' file
Comments
Post a Comment