awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk.

'{nr[$1]=$2;f = $1} END{for (i=1;i<=f;i++) if (nr[$1] != i)  print i, nr }' input1.csv >output1.csv

input1.csv

1 9
3 5
4 1
7 6
8 5
10 6

output1.csv > with the missing line number 6. 6 is $NF of last line on input1.csv

1 9
2 
3 5
4 1
5 
7 6
8 5
9 
10 6

The script is working fine but its if (nr[$1] != i) test in the END clause is more than dubious.
You are explicitly excluding the line(s) which second field is equal to the second field of the last line read, here 10 6

The script reads the first field of last line with f=$1 and not f=$NF. How can it be fixed?:confused:

It uses nr[$1] , which is $NF of the last line. What are you trying to achieve?

--
As an aside: $1 happens to be available because your particular awk retains that value in the END section. With some awks this value will be "", since the field variable are only available in the middle section.

The test is pointless, just remove it:

'{nr[$1]=$2;f = $1} END{for (i=1;i<=f;i++) print i, nr }'
1 Like

A Blank line shall be inserted where $2 is blank, while $1 shall be the NR. This is what I want to achieve:

input.csv

1 9
3 5
4 1
7 6
8 5
10 6

ouptut.csv

1 9
2 
3 5
4 1
5 
6 
7 6
8 5
9 
10 6

---------- Post updated at 03:07 PM ---------- Previous update was at 03:04 PM ----------

Thanks, wanted more than was needed!