i have a dataset which is like the following, however much larger:
5 6 9
2 4 6
4 5 1
I want to be able to subtract every field in each record from the current one, then add them together and store the results. For example here i would want, starting with the first line (5-2) + (6-4) + (9-6) = result. And also (5-4) + (6-5) and (9-1). Also do this for all other lines aswell, for example for second line (2-4) + (4-5) + (6-1) and (2-5) + (4-6) + (6-9) etc. I can do this manually as follows:
{
if (max_nf < NF)
max_nf = NF
max_nr = NR
for (x = 1; x <= NF; x++)
vector[x, NR] = $x
}
END { result = ((vector[1,1] - vector[1,2]) + (vector[2,1] - vector[2,2]) + (vector[3,1] - vector[3,2]))
}
however the dataset is large, and i would like a loop to do this, which i cannot seem to get working.
One obvious possibility would be something on this order:
Note that this makes no attempt at detecting or dealing intelligently with bad input.
Edit (to fit edited question):
Given what you’ve changed the question to ask, you apparently need to read all the data into an array, then walk through the array and add up the differences between the records. This no longer really fits very well with how awk works, so my immediate advice would be to use something else. If you insist on using awk anyway, you could basically put all the processing into the
BEGINblock, reading all the lines into a big array, then walking through it, doing all the math, and then printing the results.