Sum of numbers in row

Need help in coding:
File with several rows incl. numbers like

1 2 3 4
5 6 7 8
...

How can i build the sum of each row seperately?

10
26
...

Thx for help.

What have you tried?
How do you think you would approach this?

Unix scripting is not my daily business, therefor i cannot answer you seriously.
I think that array will be the right way. I have found a bash script. Unfortunately it includes "seq" which is not installed in my environment..

---------- Post updated at 01:57 PM ---------- Previous update was at 01:52 PM ----------

#!/bin/bash
while read line; do
a=($line)
last=$((${#a
[*]}-1))
sum=${a[0]}
echo -n "$sum "
for i in $(seq 1 $last); do         <-- this line does not work!
sum=$((sum+${a[$i]}))
echo -n "+ ${a[$i]} "
done
echo "= $sum"
done < input.txt

Hello smitty11,

Could you please try following and let me know if this helps you.

awk '{for(i=1;i<=NF;i++){NUM=NUM?NUM+$i:$i};$(NF+1)=NUM;NUM=""} 1'   Input_file

Output will be as follows.

1 2 3 4 10
5 6 7 8 26

Thanks,
R. Singh

  • bash
#!/bin/bash
while read line; do
a=($line)
sum=0
for i in "${a[@]}"; do
sum=$((sum+$i))
done
echo "= $sum"
done <input.txt
  • awk
awk '{sum=0; for(i=1; i<=NF; i++) sum += $i; print sum}'
  • Perl
perl -Mstrict -MList::Util=sum -wanE 'say sum(@F)'

Thx. Come back to you tomorrow...

---------- Post updated at 02:47 PM ---------- Previous update was at 02:38 PM ----------

By the way... how can i add up numbers in a column which are seperated by a blank line or character? Each result should be written in a outfile...

1
2
3
4
<blank line or character>
5
6
7
8

...
Result in outfile:

10
26
1
2
3
4
<blank line or character>
5
6
7
8

Are they always the same character or different one each time? More than one character?

Different characters each time but always 4 rows before blank line begins..

this simple script will add up numbers skipping every 5th line (as you have said, four rows of numbers always follow by a row we don't need to add)

#!/bin/bash
cnt=0;
sum=0;
while read n; do
        ((cnt = cnt + 1))
        if((cnt % 5)); then
                ((sum = sum + $n))
        fi
done < "$1"
echo $sum
 

As you see, there is no checking for bad input etc... just skip 5th line

Please, try the following:

perl -nle 'END{print $s} /^(\d+)/ and $s+=$1 or print $s and $s=0' smitty.file

The same a ronaldxs' suggestion, but add RS= :

awk '{sum=0; for(i=1;i<=NF;i++) sum+=$i; print sum}' RS= file

or

awk '{$1=$1}1' RS= OFS=+ file | bc 

--
Alternative for the original question:

sed 's/  */+/g' file | bc

Hi Ronaldxs , Could you please explain below code.

perl -Mstrict -MList::Util=sum -wanE 'say sum(@F)'

Works.
Thx to all.

perl                       # perl binary
-M strict                 # use strict
-M List::Util=sum    # use List::Util qw(sum).To know what the subroutine sum is.
-w                          # use warnings
-a                           # split record into array @F
-n                          # loop each line
-E                          # Load all options for the command. Necessary to make use of say instead of print
' say sum(@F)'     # take the content of array F and sum them up and display result.
1 Like