0

I have an O/p file below and I'm looking for a bash and Perl solution:

Aggregate               total   used      avail   capacity
aggr0                   100     59       41      41%
aggr1                   200     100      100     50%
aggr2                   300     150     150      50%
aggr3                   400     200      200     50%

I would like to calculate the sum of all individual column except Col-1. The final state should look like this:

Aggregate               total   used      avail   capacity
aggr0                   100     59       41      41%
aggr1                   200     100      100     50%
aggr2                   300     150     150      50%
aggr3                   400     200      200     50%
=========================================================
                        1000   509       460   50.9%  (Used % of all aggr's)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • U can find your answer here 'http://stackoverflow.com/questions/3096259/bash-command-to-sum-a-column-of-numbers' – Sander Van der Zeeuw Apr 09 '13 at 07:38
  • @SanderVanderZeeuw glad you referenced this answer, Thanks! it's interesting but doesn't quite meet requirements, I feel – Vorsprung Apr 09 '13 at 08:36
  • @Vorsprung i thought so, but the title of the question is quite the same. And the question i referred to has many answers so with a combination of them it should be possible to get the answer needed here. – Sander Van der Zeeuw Apr 09 '13 at 09:26

2 Answers2

0

infile is the file with the data in it

perl -ape 'next if $.==1; for my $i (1..4) { $s[$i] += $F[$i]; } END { $s[4]=sprintf("%.2f%%",$s[2]/$s[1]*100); $"="\t";print "total\t\t @s\n";}'
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
0

Perl is probably more suitable to this task, but it can be done from bash too :)

    tail -n +2 my_op_file.txt | while read n c1 c2 c3 c4;do
        total=$(($total + $c1 ))
        used=$(($used + $c2 ))
        avail=$(($avail + $c3 ))
        capacity=$(($capacity + ${c4%%%}))
        echo $total $used $avail $capacity
    done
Mzzl
  • 3,926
  • 28
  • 39