Ubuntu: Count the sum of each column in a file

Question:

In a file with different number of columns delimited by space ' ', How to count the sum of the columns. An example would show the need:

``File A:    1 2   2 3  4 5 6   1 1 1 5  ``

Then the output would be:

• for column 1 (1+2+4+1)=8
• for column 2 is 11
• for column 3 is 7
• for column 4 is 5

Solution:1

Using `awk`

``awk '{for (i=1;i<=NF;i++) sum[i]+=\$i;}; END{for (i in sum) print "for column "i" is " sum[i];}' FileA  for column 1 is 8  for column 2 is 11  for column 3 is 7  for column 4 is 5  ``

Solution:2

Use `numsum` for that task and separate between data processing and output the results.

Install `num-utils`, we need `numsum`

``sudo apt-get install num-utils  ``

``numsum -c <your_file_name>  ``

Example

``\$ cat "File A"  1 2   2 3  4 5 6   1 1 1 5    \$ numsum -c "File A"  8 11 7 5  ``

``\$ numsum -c "File A" | awk '{for(i=1;i<=NF;i++) {print "for column "i" is "\$i}}'  for column 1 is 8  for column 2 is 11  for column 3 is 7  for column 4 is 5  ``

from `man numsum`

``-c      Print out the sum of each column.  ``

examples from `man numsum`

``EXAMPLES       Add up the 1st, 2nd and 5th columns only.           \$ numsum -c -x 1,2,5 columns         15 40 115       Add up the rows of numbers of a file.            \$ numsum -r columns          55          60          65          70          75  ``

Solution:3

``#!/bin/sh    while read a b c d; do      col1=\$((col1 + a))      col2=\$((col2 + b))      col3=\$((col3 + c))      col4=\$((col4 + d))  done < File_A    echo \$col1 \$col2 \$col3 \$col4  ``

Solution:4

Judging by the comments to your own answer, you only want the sum of one column at a time. If so, here is a non-awk way to do it:

``cut -d' ' -f3 FileA | grep . | paste -s -d+ | bc  ``

where you would replace the `3` with the column number you are interested in.

Solution:5

Here's a one-liner Perl script approach. This relies on the use of `-a` flag which allows auto-splitting currently read line with `-n` flag into array `@F`. All we have to do is iterate over those items, and add them to their respective index in `\$sum` array, thus effectively each array item is sum for each respective column. Finally, we print the result within `END` code block.

``\$ perl -lane '\$j=0;foreach \$i (@F){\$sum[\$j]+=\$i; \$j+=1;}; END{print join("\n",@sum)} ' input.txt                                                       8  11  7  5  ``

Alternatively, here's a full Perl script approach. It relies on splitting each line into array, and iterating over each item in that array adding each number to their respective possition in `@sums` array. The script prints out each line, then produces report for each column. Printing of each line can be removed by adding `#` before `printf("%s",\$line);`

``#!/usr/bin/env perl  use strict;  use warnings;    open(my \$fh,"<",\$ARGV[0]);   my \$i = 0;  my @sums;    while(my \$line = <\$fh>) {       printf("%s",\$line);      my @nums = split(" ",\$line);      my \$j = 0;      foreach my \$num (@nums){          \$sums[\$j] += \$num;          \$j += 1;      }    }    my \$k = 0;  foreach my \$sum (@sums){      printf("- column %d sum: %d\n",\$k,\$sum);      \$k+=1;  }    close(\$fh);  ``

The usage is simple `chmod +x ./sum_columns.pl && ./sum_columns.pl input.txt`. For example:

``\$ ./sum_columns_2.pl input.txt                                                                                                                         1 2   2 3  4 5 6   1 1 1 5  - column 0 sum: 8  - column 1 sum: 11  - column 2 sum: 7  - column 3 sum: 5  ``

Solution:6

A simple solution:

``awk '{sum += \$i} END {print sum}' file  ``

Replace i with column number for example column1:

``awk '{sum += \$1} END {print sum}' file  ``

output is:

``8  ``

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »