Reputation: 637
File 1:
1 0.3
2 0.1
3 0.4
4 0.8
File a:
2 0.7
4 0.2
6 0.5
8 0.9
File b:
2 0.4
6 0.6
Step-by-step:
Desired output in File 2:
1 0.3
2 1.2
3 0.4
4 1.0
Namely, File 2 = File 1, except the rows, where $1 in File 1 matches $1 in File a or b, have been added together in $2.
I have already received advice on this problem for matching and adding two files, using the code
awk 'FNR==NR{a[$1]=$2;next} {$2=$1 in a?$2+a[$1]:$2} 1' Input_file2 Input_file1 >> output_file
However, the code cannot process 3 input files simultaneously. If I do them separately (two files at a time, ie. File 1 + File a, then File 1 + File b), I end up adding File 1's value twice.
As I potentially have 3-6 files to compare, I cannot simply do File a + File b >> File c, followed by File 1 + File c >> File 2.
I would like a script that can search for matches in $1 between three files, then print $2 (File 1) + $2 (File a and/or File b) wherever a $1 match is found. The output is File 2, which prints File 1 with the new summed values in $2 whereever matches in $1 occurred. Any assistance is much appreciated!
Upvotes: 1
Views: 80
Reputation: 37394
Here in one:
$ awk '
NR==FNR { # store the file1 to a hash and add to its elements
a[$1]=$2
next
}
($1 in a) {
a[$1]+=$2
}
END {
for(i in a)
print i,a[i] # print in no particular order
}' file1 filea fileb
Output:
1 0.3
2 1.2
3 0.4
4 1
Upvotes: 1