Sum up numbers in a for loop

Hi

i have to calculate some numbers, column by column.
Herfore i used a for-loop..

for i in {4..26};do awk -F"," '{x'$i'+=$'$i'}END{print '$i'"\t" x'$i'}' file.tmp;done
----- printout -----
4	660905240
5	71205272
6	8.26169e+07
7	8.85961e+07
8	8.60936e+07
9	7.42238e+07
10	5.6051e+07
11	3.79546e+07
12	2.5881e+07
13	1.82641e+07
14	1.32864e+07
15	1.02451e+07
16	8.09118e+06
17	6.51037e+06
18	5.35195e+06
19	4.40399e+06
20	3.56908e+06
21	2.90443e+06
22	2.40737e+06
23	2.03822e+06
24	1.82266e+06
25	1.85219e+06
26	2.10968e+06

and it works fine. furthermore i need the sum over the calculated second result column. i want to try to do it in "one-line". can someone help me please?

Thanks in advance,
IMPe

What are the contents of file.tmp?

1 Like

Hi Corona688,
first of all, thanks for supporting me.
file.tmp is a csv-file.
sample-nr in column $1
date in column $2
time in column $3
from $4 ...$26 are columns with measurement-values

...
252,01/22/14,10:30,454714,40308,45974,48919,47693,40963,30404,18882,10975,  ...
253,01/22/14,10:40,454764,41308,42914,48922,47634,40955,30123,19922,11254,  ...
...

How about you tell us exactly what you are trying to do? I don't think your original solution was the most efficient, running awk once per line.

1 Like

Try sth like

awk -F, '{for (i=4;i<=26;i++) SUM+=$i} END {for (i=4;i<=26;i++) print i, SUM}' OFS="\t" file
1 Like

OK, thanks for that. but after the summation of the several colums from file.tmp i want summing up the second column of the result - in blue.

4	660905240
5	71205272
6	8.26169e+07
7	8.85961e+07
...
25	1.85219e+06
26	2.10968e+06

Sorry?

awk -F, '{for (i=4;i<=26;i++) {SUM+=$i;t+=$i}} END {for (i=4;i<=26;i++) print i, SUM; print "\ntotal: " t}' OFS="\t" file