I have four columns input data.x,y,z b,c,d are some numbers not necessarily the same.
The data could be separated in blocks of triplets for which the third column is the same.
The sequence z1,z2,z3 would show three times in that order.
What is important is that the data always follows this patern.
There are no blank lines in the data. I put the blank line here for clarification. Here is actual test data.
There are three cases for the first two columns.
Both zero. Only one of the $1 or $2 non zero.
The third column is always nonzero.
The operation I would like to perform on each triplet of data goes like that.
Get the third column values and subtract the middle fourth column from its two neighbors.
The first block with zeros for $1 and $2 would subtract fourth column from the previous and the following block.
0.04 0.97-0.06-0.01
0.05 0.96-0.05-0.02
0.06 0.95-0.04-0.03
The second triplet of data would be processed in a similar way:
0.07 0.94-0.03-0.03
0.08 0.93-0.04-0.02
0.09 0.92-0.05-0.01
Desired output is:
My approach to this is the following.
Define 2D array.
The index of this array gets zeroed every time when I go from one block of triplets to another.The third data column would be the second index of this array.
My code so far.
So In the first two {....} I update the array
In the third {...} I update the array and print it because the test for the beginning of new block evaluates to true.
I hope that make sense.
Thanks.
Code:
x 0 z1 bl
x 0 z2 cl
x 0 z3 dl
0 0 z1 bc
0 0 z2 cc
0 0 z3 dc
0 y z1 br
0 y z2 br
0 y z3 br
The sequence z1,z2,z3 would show three times in that order.
What is important is that the data always follows this patern.
There are no blank lines in the data. I put the blank line here for clarification. Here is actual test data.
Code:
4 0 0.04 0.06
6 0 0.05 0.05
2 0 0.06 0.04
0 0 0.04 0.97
0 0 0.05 0.96
0 0 0.06 0.95
0 3 0.04 0.01
0 6 0.05 0.02
0 1 0.06 0.03
7 0 0.07 0.03
2 0 0.08 0.04
5 0 0.09 0.05
0 0 0.07 0.94
0 0 0.08 0.93
0 0 0.09 0.02
0 3 0.07 0.03
0 6 0.08 0.02
0 1 0.09 0.01
Both zero. Only one of the $1 or $2 non zero.
The third column is always nonzero.
The operation I would like to perform on each triplet of data goes like that.
Get the third column values and subtract the middle fourth column from its two neighbors.
The first block with zeros for $1 and $2 would subtract fourth column from the previous and the following block.
0.04 0.97-0.06-0.01
0.05 0.96-0.05-0.02
0.06 0.95-0.04-0.03
The second triplet of data would be processed in a similar way:
0.07 0.94-0.03-0.03
0.08 0.93-0.04-0.02
0.09 0.92-0.05-0.01
Desired output is:
Code:
0.04 0.90
0.05 0.89
0.06 0.88
0.07 0.88
0.08 0.87
0.09 0.86
Define 2D array.
The index of this array gets zeroed every time when I go from one block of triplets to another.The third data column would be the second index of this array.
My code so far.
Code:
BEGIN{l=0;r=0;c=0}
$1==0&&$2==0{c=1; } #In these two brackets I update the array
$1==0&&$2!=0{r=1; } #
$1!=0&&$2==0{l=1; if (r!=0 ) print "New Block begins at "$3 ;r=0 }
In the third {...} I update the array and print it because the test for the beginning of new block evaluates to true.
I hope that make sense.
Thanks.