awk problem

Hi

I have two columns and I would like to create a third column based on how many lines away from a value of 1 in column 2,

for example I have

1,0
2,0
3,0
4,0
5,0
6,1
7,0
8,0
9,0
10,0
11,1

And I want an output

1,0,5
2,0,4
3,0,3
4,0,2
5,0,1
6,1,0
7,0,1
8,0,2
9,0,2
10,0,1
11,1,0

I'm stumped!

Many thanks in advance

In situations where you need to 'think back' like this I find it helpful to read the file twice... Once to figure it out, then again to modify and print.

$ awk -F, -v OFS="," 'NR==FNR { if($2 == 1) L++ ; A[L+0]++; next } # All lines of first file
FNR==1 {L=0} # First line of second file
{ if(A[L]<0) L++; $3=A[L]--; } 1' input input

1,0,5
2,0,4
3,0,3
4,0,2
5,0,1
6,1,0
7,0,5
8,0,4
9,0,3
10,0,2
11,1,1

$

For the first file, it counts up A[L] for each and every line. Every time it finds a 1, it increases L by one too. So A[0] is a count of how many lines were between it and the first one, etc.

The second file it does the exact opposite, counting down. Whenever A[L] goes negative, it increments L one.

It's not bidirectional like yours though yet. You might need to keep two counters and take the minimum.

Looking through some of your earlier threads, it appears that you never use CODE tags, you never let us know if suggestions given to you work, and you almost never thank the volunteers here who try to help answer your questions.

It also seems that you want the volunteers here to act as your unpaid programming staff rather than to act as advisors to help you learn how to do your own coding.

This is an interesting puzzle, and Corona688 gave you a good starting point. I had to think about what you were trying to do for about ten minutes before I figured out a way to get what you want.

Can you explain the logic needed to solve your problem (even if you can't write the code for it)? What did you try to get the results you're looking for before you were stumped?

Founded that your need a minimum number, it frustrated me.

$ awk '{a[NR]=$0}$2=="1"{p=s+1;s=NR;for(i=p;i<=s;i++){if(p==1){print a","s-i}else{x=i-p+1<s-i?i-p+1:s-i;print a","x}}}' FS=',' file
1,0,5
2,0,4
3,0,3
4,0,2
5,0,1
6,1,0
7,0,1
8,0,2
9,0,2
10,0,1
11,1,0
1 Like

@Lucas, there would be an issue still if the field2 in the last line is 0...

1 Like

Hi Scrutinizer,
Thanks for reminding me, when I was coding, I felt that I am missing something like bound check, but when I saw the result is correct and there is another thing disturbed me then.
Apologize for my mistake. After added some code it may be fixed.

awk '{a[NR]=$0}$2=="1"{p=s+1;s=NR;for(i=p;i<=s;i++){if(p==1){print a","s-i}else{x=i-p+1<s-i?i-p+1:s-i;print a","x}}}
END{if(s!=FNR){for(i=s+1;i<=FNR;i++){print a","i-s}}}' FS=',' file
1 Like

Hi Lucas, no reason to apologize at all !

It appears to work now :b:,

You might want to use more vertical real estate, it is a bit too long for one line