I have two files.
file1
NC_000001.11_NM_001005484.2 69270 234 69037
NC_000001.11_NM_001005484.2 69511 475 69037
NC_000001.11_NM_001005484.2 69761 725 69037
NC_000001.11_NM_001385640.1 942155 20 942136
file2
NC_000001.11_NM_001005484.2 65565 9
NC_000001.11_NM_001005484.2 69037 969
NC_000001.11_NM_001385640.1 924432 517
NC_000001.11_NM_001385640.1 925922 92
NC_000001.11_NM_001385640.1 930155 182
NC_000001.11_NM_001385640.1 931039 51
NC_000001.11_NM_001385640.1 935772 125
NC_000001.11_NM_001385640.1 939040 90
NC_000001.11_NM_001385640.1 939272 141
NC_000001.11_NM_001385640.1 941144 163
NC_000001.11_NM_001385640.1 942136 116
NC_000001.11_NM_001385640.1 942410 79
NC_000001.11_NM_001385640.1 942559 500
NC_000001.11_NM_001385640.1 943253 125
NC_000001.11_NM_001385640.1 943698 111
NC_000001.11_NM_001385640.1 943908 243
If column 1 in file 1 matches column 1 in file 2 AND the value in column 2 of file 2 is less than the value of column 4 in file 1, I would like to sum column 3 of file 2 over the matching keys. Then I would like to print each line from file1 with its corresponding sum from file2.
Expected output
NC_000001.11_NM_001005484.2 69270 234 69037 9
NC_000001.11_NM_001005484.2 69511 475 69037 9
NC_000001.11_NM_001005484.2 69761 725 69037 9
NC_000001.11_NM_001385640.1 942155 20 942136 1361
I'm not experienced enough with awk or python to get this right but have tinkered around for a couple of days now. I would appreciate any help with this.