简体   繁体   English

bash中多列的总和

[英]Sum of multiple columns in bash

I have an O/p file below and I'm looking for a bash and Perl solution: 我下面有一个O / p文件,正在寻找bash和Perl解决方案:

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. 我想计算除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)

infile是其中包含数据的文件

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";}'

Perl is probably more suitable to this task, but it can be done from bash too :) Perl可能更适合此任务,但是也可以通过bash来完成:)

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM