Update a file using other file

Gents,

Please help.
Matching fields 2 and 3 in file1 and file2, I will like to update the file 2 using the information (column 48 to 71) from file 1

examples

file1

S  40149.00  47897.00  11                      453310.8 2443363.8 131.9348000413                               
S  40573.00  47897.00  11                      453312.3 2448664.5 131.4348000422                               
S  40147.00  47897.00  11                      453311.9 2443338.5 132.2348000450                               
S  40575.00  47897.00  11                      453311.9 2448687.9 131.2348000458                               
S  40145.00  47897.00  11                      453312.7 2443313.0 131.9348000526                               

File2

S  40149.00  47897.00  1V1     0.0   0     0.0 453312.5 2443362.5   0.0348000414                               
S  40147.00  47897.00  1V1     0.0   0     0.0 453312.5 2443337.5   0.0348000451                               
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.5 2443312.5   0.0348000527                               
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.5 2448662.5   0.0348000423                               
S  40575.00  47897.00  1V1     0.0   0     0.0 453312.5 2448687.5   0.0348000459                               

output

S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414                               
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451                               
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527                               
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423                               
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459                              

Thanks for your help.

awk 'FNR==NR {k[$2,$3]=substr($0, 48, 24);next} ($2,$3) in k{sub(substr($0, 48, 24), k[$2,$3])}1' file1 file2
1 Like
$ 
$ cat file1
S  40149.00  47897.00  11                      453310.8 2443363.8 131.9348000413
S  40573.00  47897.00  11                      453312.3 2448664.5 131.4348000422
S  40147.00  47897.00  11                      453311.9 2443338.5 132.2348000450
S  40575.00  47897.00  11                      453311.9 2448687.9 131.2348000458
S  40145.00  47897.00  11                      453312.7 2443313.0 131.9348000526
$ 
$ cat file2
S  40149.00  47897.00  1V1     0.0   0     0.0 453312.5 2443362.5   0.0348000414
S  40147.00  47897.00  1V1     0.0   0     0.0 453312.5 2443337.5   0.0348000451
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.5 2443312.5   0.0348000527
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.5 2448662.5   0.0348000423
S  40575.00  47897.00  1V1     0.0   0     0.0 453312.5 2448687.5   0.0348000459
$ 
$ perl -F'/\s+/' -lane '$ARGV eq "file1"
                        ? $x{join("|",@F[1,2])} = substr($_,47)
                        : do {$k = join("|",@F[1,2]); substr($_,47) = $x{$k} if defined $x{$k}; print}
                       ' file1 file2
S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000413
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000450
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000526
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000422
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000458
$ 
$ 
2 Likes

Hi, durden_tyler

Nice work, however your output is a bit off of what it is wanted if I am interpreting correctly.

  Your output: S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000413
Wanted output: S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414 
  Your output: S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000450
Wanted output: S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451
  Your output: S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000526
Wanted output: S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527
  Your output: S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000422
Wanted output: S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423
  Your output: S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000458
Wanted output: S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459

I was thinking more in the following line:

perl -anle '$ARGV eq "file1" ? $p{"@F[1,2]"} = substr($_, 47, 24) : do{ if(defined $p{"@F[1,2]"}){substr($_, 47, 24, $p{"@F[1,2]"})}print}' file1 file2

Output:

S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459
2 Likes

Thanks for pointing it out, Aia.
jiam912's specification was very precise, but I missed it.
Pretty good use of replacement within substr() function in your code.
Cheers!

1 Like

Aia,

Thanks a lot