Match string in two files and add data to one file

Gents,

file1

S  65733.00  19793.00  1              0        318592.8 2792489.5  29.1063000008
S  65733.00  19801.00  1              0        323120.8 2789153.6  13.3063000044
S  66009.00  19713.00  1              0        318672.7 2792538.2  30.6063000120
S  65801.00  19799.00  1              0        323136.5 2789108.6  13.3063000156
S  65733.00  19747.00  1              0        318650.8 2792581.2  30.2063000232
S  65733.00  19753.00  1              0        318631.5 2792607.3  29.7063000330
S  65799.00  19799.00  1              0        323160.6 2789067.9  13.6063000424
S  66015.00  19713.00  1              0        318598.4 2792665.9  30.0063000500
S  65733.00  19761.00  1              0        323216.1 2789042.9  15.9063000520
S  65733.00  19765.00  1              0        318575.0 2792706.1  30.0063000614
S  65795.00  19801.00  1              0        323248.5 2789007.8  19.4063000632
S  66019.00  19713.00  1              0        318551.2 2792752.3  30.3063000838
S  65797.00  19797.00  1              0        323149.6 2788993.6  15.1063000856
S  66019.00  19721.00  1              0        318727.0 2792852.7  31.9063000959
S  65733.00  19773.00  1              0        323167.6 2788954.7  17.5063001017
S  65733.00  19775.00  1              0        318749.1 2792810.5  31.1063001053

file2

   65733.00  19747.00  1221
   65733.00  19753.00  1221
   65733.00  19761.00  1222
   65733.00  19765.00  1222
   65733.00  19773.00  1223
   65733.00  19775.00  1223
   65733.00  19793.00  1224
   65733.00  19801.00  1224

output

S  65733.00  19793.00  1              0        318592.8 2792489.5  29.1063000008 1224
S  65733.00  19801.00  1              0        323120.8 2789153.6  13.3063000044 1224
S  66009.00  19713.00  1              0        318672.7 2792538.2  30.6063000120
S  65801.00  19799.00  1              0        323136.5 2789108.6  13.3063000156
S  65733.00  19747.00  1              0        318650.8 2792581.2  30.2063000232 1221
S  65733.00  19753.00  1              0        318631.5 2792607.3  29.7063000330 1221
S  65799.00  19799.00  1              0        323160.6 2789067.9  13.6063000424
S  66015.00  19713.00  1              0        318598.4 2792665.9  30.0063000500
S  65733.00  19761.00  1              0        323216.1 2789042.9  15.9063000520 1222
S  65733.00  19765.00  1              0        318575.0 2792706.1  30.0063000614 1222
S  65795.00  19801.00  1              0        323248.5 2789007.8  19.4063000632
S  66019.00  19713.00  1              0        318551.2 2792752.3  30.3063000838
S  65797.00  19797.00  1              0        323149.6 2788993.6  15.1063000856
S  66019.00  19721.00  1              0        318727.0 2792852.7  31.9063000959
S  65733.00  19773.00  1              0        323167.6 2788954.7  17.5063001017 1223
S  65733.00  19775.00  1              0        318749.1 2792810.5  31.1063001053 1223

I got the desired output with this.

awk '{x=substr($0,1,80)} FNR==NR{a[substr($0,4,15)]=$3;next} {print x, a[substr($0,4,15)] }' file2 file1 

.

But what about if i have file3 is like this

65733.00  19747.00  1221   
65733.00  19753.00  1221   
65733.00  19761.00  1222   
65733.00  19765.00  1222   
65733.00  19773.00  1223   
65733.00  19775.00  1223   
65733.00  19793.00  1224   
65733.00  19801.00  1224

Where the string 1-15 are not in the same location in file2 where this string is in columns 4-18..

I notice that the code above works only if the values to match are exactly in the same location.. How to solve it if the values to match are in different locations as the example. Then how I can get the same output using file3 and file1.

Thanks for your help

Use fields instead. The fields do not care about the number of spaces, just the order, which in this case is maintained.

awk 'FILENAME=="file3" { arr [$1 $2]=$3}
         FILENAME=="file1" { 
                               string=""
                               if( "$2 $3" in arr ){string=arr[$2 $3]}
                               print $0, string 
                             } '  file3 file1
1 Like

Thanks :slight_smile: