Sum of all rows of all columns - Bash

后端 未结 6 409
鱼传尺愫
鱼传尺愫 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条回答
  • 2021-01-20 03:48

    There is a very simple command called numsum to do this:

    numsum -c FileName
    
    -c   ---    Print out the sum of each column.
    

    For example:

    cat FileName
    1 4 7 
    2 5 8
    3 6 9 
    

    Output :

    numsum -c FileName
    6 15 24
    

    Note: If the command is not installed in your system, you can do it with this command:

    apt-get install num-utils
    
    0 讨论(0)
  • 2021-01-20 03:49
    echo "1 4 7
    2 5 8
    3 6 9 " \
    | awk '{for (i=1;i<=NF;i++){
       sums[i]+=$i;maxi=i}
     }
     END{
       for(i=1;i<=maxi;i++){
         printf("%s ", sums[i])
       }
     print}'
    

    output

    6 15 24
    

    My recollection is that you can't rely on for (i in sums) to produce the keys any particular order, but maybe this is "fixed" in newer versions of gawk.

    In case you're using an old-line Unix awk, this solution will keep your output in the same column order, regardless of how "wide" your file is.

    IHTH

    0 讨论(0)
  • 2021-01-20 03:51

    One more awk

    awk '{for(i=1;i<=NF;i++)$i=(a[i]+=$i)}END{print}' file
    

    Output

    6 15 24
    

    Explanation

    {for (i=1;i<=NF;i++)         Set field to 1 and increment through
    
    $i=(a[i]+=$i)                Set the field to the sum + the value in field
    
    END{print}                   Print the last line which now contains the sums
    

    As with the other answers this will retain the order of the fields regardless of the number of them.

    0 讨论(0)
  • 2021-01-20 03:52

    Your answer is correct. It is just missed to print "sum". Try this:

    awk '{sum+=$1;} END{print sum;}' infile > outfile
    
    0 讨论(0)
  • 2021-01-20 03:55

    You want to sum every column differently. Hence, you need an array, not a scalar:

    $ awk '{for (i=1;i<=NF;i++) sum[i]+=$i} END{for (i in sum) print sum[i]}' file
    6
    15
    24
    

    This stores sum[column] and finally prints it.

    To have the output in the same line, use:

    $ awk '{for (i=1;i<=NF;i++) sum[i]+=$i} END{for (i in sum) printf "%d%s", sum[i], (i==NF?"\n":" ")}' file
    6 15 24
    

    This uses the trick printf "%d%s", sum[i], (i==NF?"\n":" "): print the digit + a character. If we are in the last field, let this char be new line; otherwise, just a space.

    0 讨论(0)
  • 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 
    
    0 讨论(0)
提交回复
热议问题