Q:

Sum one column based on a pattern in another AWK

My input file is as follows:
scaffold100 115050  116050  1000    115050  124008  2
scaffold100 1167811 1168811 1000    1167811 1568661 0
scaffold100 151385  152385  1000    151385  193501  4
scaffold100 1585695 1586695 2000    1585695 1644609 5
scaffold100 1654143 1655143 2000    1654143 1673659 3
scaffold100 1711976 1712976 2000    1711976 1843374 1

I am learning AWK and I want to use it to sum column 7 based on column 4.
That is if column 4 is equal to 1000, then sum up 2+0+4=6 and print. 
I am tyring this code but I don't understand what I am doing wrong:
awk '{if($4==1000) {sum+=$7;n++}END{print sum}}' file

A:

You can use this awk:
awk '$4==1000{sum+=$7}END{print sum+0}' file

Output:
$ awk '$4==1000{sum+=$7}END{print sum+0}' a
6

Another awk:
awk '{arr[$4]+=$7}END{for(val in arr){print val,arr[val]}}' file

Output:
$ awk '{arr[$4]+=$7}END{for (val in arr) {print val,arr[val]}}' a
2000 9
1000 6

