ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

I'm struggling myself with this problem: I have a file which contains a single column of data

2
.
.
.
45
65
.
.
.
23
87
.
.
.
43

I would like to have a results of the sum of the first n lines, then a results of the sum of the second n lines, then a results of the sum of the third n lines, and so on... all printed in a single column

$ ruby -ne 'BEGIN{s=0};s+=$_.to_i ; if $.%5==0 then puts s;s=0 ; end ' file

it says initialize s=0, accumulate s as we go along, check if the line number is mod 5 is 0 (every 5th line) , if it is , output s value and reset. Adapt it to which ever language/tool you are using.

What have you tried and where are you getting stuck?
Remember the idea is we are here to help, not just do it for you.

I have tried with the following

awk '{ line[NR] = $0 }; sum = 0
END { for (t = 1; t <= 2; t++)
for (i = 1; i <= NR; i++) {
n = split(line[i], field)
printf(" %g", s=field[1]*field[3]/t+field[2]*field[4]/t)
sum = sum + s; printf(" %g", sum)
printf("\n")
}
}' proba.dat > proba1.dat

proba.dat contains the data. I have stuck with the issue that I just don't know how to make it stop summing when the first iteration over i ends, and start the new iteration with the sum=0. My try give me the sums of all the previous ones. And its in the 2nd column (but this is not such a big issue).

$ ruby -ne 'BEGIN{s=0};s+=$_.to_i ; if $.%5==0 then puts s;s=0 ; end ' file

it says initialize s=0, accumulate s as we go along, check if the line number is mod 5 is 0 (every 5th line) , if it is , output s value and reset. Adapt it to which ever language/tool you are using.

Thanks!
What I get is

-e:1: undefined method `+' for nil:NilClass (NoMethodError)

Following the suggestion by kurumi, you can check if the line number is a multiple of 5 and you can do it on-the-fly without storing all the file content in memory and parse it at the end. Example:

Following the suggestion by kurumi, you can check if the line number is a multiple of 5 and you can do it on-the-fly without storing all the file content in memory and parse it at the end. Example:

Yes, I was just about playing with the if as kurumi proposed. So, taking both of yours advices, I have in the end (for the whole problem)

awk '{ line[NR] = $0 }
END { for (t = 1; t <= 2; t++)
for (i = 1; i <= NR; i++) {
n = split(line[i], field)
s=field[1]*field[3]/t+field[2]*field[4]/t
sum += s
printf(" %g", sum); sum=0
printf("\n")
}
}' proba.dat > proba1.dat

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.