Sum of all rows of all columns - Bash

后端 未结 6 414
鱼传尺愫
鱼传尺愫 2021-01-20 02:54

I have a file like this

1 4 7 ...
2 5 8
3 6 9 

And I would like to have as output

6 15 24 ...

That is the

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 04:01

    AWK Program

    #!/usr/bin/awk -f
    
    {
        print($0);
        len=split($0,a);
        if (maxlen < len) {
            maxlen=len;
        }
        for (i=1;i<=len;i++) {
            b[i]+=a[i];
        }
    }
    
    END {
        for (i=1;i<=maxlen;i++) {
            printf("%s ", b[i]);
        }
        print ""
    }
    

    Output

    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    3 6 9 12 15 
    

提交回复
热议问题