Wednesday, September 12, 2012

8 examples to find sum of all columns / numbers in a line



How to find the sum of all numbers or columns in a line of a text / CSV file?
    In an earlier article, we discussed about different ways of how to sum all the numbers in a file. In this, we will see how to find the sum of all the numbers in a line, or in other words, sum of all the columns in a line.

Let us consider a file with the following contents:
$ cat file
20 30 21
33 33 32
12 21 12
1. sed solution:
$ sed 's/ /+/g' file | bc
71
98
45
Using the simple substitution(s) in sed, all spaces in the file is replaced with '+'. And the string is passed to the bc command which gives the sum of the numbers present in the line. In case of file being comma separated instead of space separated, just replace the space with the comma:
$ sed 's/,/+/g' file | bc
2. awk solution:
$ awk '{print $1+$2+$3}' file
71
98
45
Simply print the sum of all the fields. Since we have 3 fields, the sum of $1, $2 and $3 gives the sum of all the numbers of the line. However, this approach wil be tedious if there were to present lots of numbers in a line.
  In case, say the input file is a comma delimited file:
$ awk -F, '{print $1+$2+$3}' file
3. awk solution for lot of numbers in a line:
$ awk '{for(i=1;i<=NF;i++)x+=$i;print x}' file
71
169
214
 The earlier awk solution won't be feasible if there are lots of numbers in a line, and hence this way. A loop is run on the entire list of columns. And each value present in the column is added to the variable x, which in the end of the loop contains the sum of all the numbers in the line.

4. Bash Shell script for adding all numbers in a line:
$ while read x1 x2 x3
> do
>   x=$((x1+x2+x3))
>   echo $x
> done < file
71
98
45
Using while loop, every column can be read into a variable. And the varaibles are summed up using the $(()) notation.

5. Shell Script(ksh/bash):
$ typeset -i x
$ while read line
> do
>  x=`echo $line | sed 's/ /+/g'`
>  echo $x
> done < file
71
98
45
typeset is very useful for arithmetic operations. typeset -i declares a variable as an integer. And hence any mathematical terms automatically get evaluated. As shown here, if x is assigned "1+2+3", on echoing $x, 6 will be printed.

6. expr command:
$ while read line
> do
>  x=`echo $line | sed 's/ / + /g'`
>  expr $x
> done < file
71
98
45
The same as the last one, the only difference being using the expr command for addition.

7. Perl solution:
$ perl -anle '$x+=$_ for(@F);print $x;$x=0;' file
71
98
45
The option 'a' of perl automatically splits the line and stores the invidual elements in the array @F. Using for loop, the individual elements of the array are added and the result is printed.

8. Perl using eval:
$ perl -anle 'print eval join "+",@F;' file
71
98
45
join function joins all the elements of the array using "+" as delimiter. And the eval function evaluates the arithmetic expression and the result gets printed.

4 comments:

  1. Is there a way to not do the cumulative sum (Output differs from other methods) in Method 3 above ?

    ReplyDelete
  2. In solution 3 , the re-initialization of X is missing unless you intended on having it like this if not to have an answer like solutions.

    awk '{for(i=2;i<=NF;i++)x+=$i;print $1,x;x=0}'

    ReplyDelete
  3. Nice examples, however it should be said which way is the fastest one

    ReplyDelete