File 1:
1 0.3
2 0.1
3 0.4
4 0.8
File 2:
2 0.7
4 0.2
6 0.5
8 0.9
Examining field 1 in both File 1 and File 2, we see the strings 2 and 4 are in common. These are my reference rows. For these reference rows, I would like to add the values from field 2 in both files.
In other words,
search File 1 and File 2 for matching strings in $1. In this case, 2 and 4.
for $1 = 2, then $2 = 0.1 + 0.7 = 0.8
for $1 = 4, then $2 = 0.8 + 0.2 = 1.0
Desired output in File 3:
1 0.3
2 0.8
3 0.4
4 1.0
Namely, File 3 = File 1, except the rows, where $1 in File 1 matches $1 in File 2, have been added together in $2.
Summary
I would like a script that can search for matches in $1 between two files, then print $2 (File 1) + $2 (File 2) wherever a $1 match is found. The output is File 3, which prints File 1 with the new summed values whereever matches occurred. Any assistance is much appreciated!
sort+join+ (cut+sed+bc) or +awk