In this case N=2, but it can be what ever number.
I would like to replace
the 0's in column 1 with the sum of the column N+1,2N+1,3N+1
the 0's in column 2 with the sum of the column N+2,2N+2,3N+2
...
the 0's in column N with the sum of the column N+N,2N+N,3N+N
At the moment I'm using the following script which works fine if N=1
I did my best to not make any assumptions regarding your assumptions (I was somewhat puzzled by the choice of negative three to trigger the printing of a record, but I kept it ;))
---------- Post updated at 03:01 PM ---------- Previous update was at 12:29 PM ----------
As i understand the problem, ahmad.diab's solution is incorrect but it did make me realize that i was over-engineering. A simpler approach inspired by his post (the only advantage of the modulus-based solution above is that it can readily handle MxN, and not just 4xN, if the hardcoded 4 is parameterized).
The following gives the same output given the same sample data files (data3 and data4) used above.
$ awk '/^[0-9]/ && $1==0 && (N=NF/4) { for (i=1;i<=N;i++) $i=sprintf("%.4E", $(N+i)+$(2*N+i)+$(3*N+i))} -3' OFS='\t' data
I don't know. heheh. In the original post's code, it triggers printing of a record that doesn't match the criteria which seem to identify records with data to process. I couldn't part with it.
Technically, any pattern expression that evaluates to non-zero/true will cause its corresponding action to execute. -3 evaluates to true and its corresponding action is absent and so it default to printing $0.
The number of columns is 4*N. The problem statement says that they are divided into four groups. The only columns that will be modified to contain the sum of their counterparts are the first N. Your code is going beyond the first N columns and modifying ones it shouldn't.
Thank you for all the reply.
I got from Radoulov the idea of using awk with -3 option although I didn't know what -3 meant. So any other solution is welcome.
I tried the solution
awk '/^[0-9]/ && $1==0 && (N=NF/4) { for (i=1;i<=N;i++) $i=sprintf("%.4E", $(N+i)+$(2*N+i)+$(3*N+i))} -3' OFS='\t' data
but I get as the output the input itself.
I tried also the first solution of alister which he showed to be working but I still get as the output the input itself.
Maybe the =0 is not detected with floating point?
---------- Post updated at 04:33 AM ---------- Previous update was at 04:16 AM ----------
Sorry there was a mistake in the input file (1 extra tab before the first column). now it's fixed.
thank you again,
sarah
Thanks alister for the clarification but as you can see f_o_555 post was not
very clear to me, and as per my code was modified and used by you it means
that I was on the correct track and then misunderstand the post.