0

I have two files:

File1:

cluster17 7 Rde_Locus_34636 Rde_Locus_39558 Rde_Locus_44272 Rde_Locus_20977 Rde_Locus_50929 Rph_Locus_10923 Rph_Locus_65326 
cluster18 7 Rde_Locus_1381 Rde_Locus_11734 Rde_Locus_4781 Rph_Locus_26879 Rph_Locus_7821 Rde_Locus_533 Rde_Locus_21493 
cluster19 7 Rde_Locus_6450 Rde_Locus_10727 Rde_Locus_1801 Rde_Locus_25045 Rph_Locus_13685 Rph_Locus_6953 Rph_Locus_22248 
cluster21 7 Rde_Locus_23138 Rde_Locus_10462 Rph_Locus_10145 Rph_Locus_49405 Rde_Locus_42639 Rph_Locus_70757 Rde_Locus_36462 

File2:

Rde_Locus_36462 M2
Rde_Locus_42639 S1
Rde_Locus_4781 M5
Rde_Locus_533 M3
Rde_Locus_6450 M1
Rph_Locus_10145 S4
Rph_Locus_13685 M2
Rph_Locus_22248 M4
Rph_Locus_26879 S5

I'd like to join the two files so that the output would be:

cluster17 7 Rde_Locus_34636 Rde_Locus_39558 Rde_Locus_44272 Rde_Locus_20977 Rde_Locus_50929 Rph_Locus_10923 Rph_Locus_65326 
cluster18 7 Rde_Locus_1381 Rde_Locus_11734 Rde_Locus_4781_M5 Rph_Locus_26879_S5 Rph_Locus_7821 Rde_Locus_533_M3 Rde_Locus_21493 
cluster19 7 Rde_Locus_6450_M1 Rde_Locus_10727 Rde_Locus_1801 Rde_Locus_25045 Rph_Locus_13685_M2 Rph_Locus_6953 Rph_Locus_22248_M4 
cluster21 7 Rde_Locus_23138 Rde_Locus_10462 Rph_Locus_10145_S4 Rph_Locus_49405 Rde_Locus_42639_S1 Rph_Locus_70757 Rde_Locus_36462_M2

that is, append a _$2 from File2 to the corresponding match in File1. I guess the answer is some sort of grep -f and sed combination, but I'm struggling with it. Note that I can't use join, as I can't sort File1.

1

Here's one possible way - a lookup table using an awk associative array:-

awk '
  NR==FNR {
    a[$1] = $1 "_" $2; next;
  } 
  {
    for (i=1;i<=NF;i++) {if ($i in a) $i = a[$i];} 
  }
  1' file2 file1

There are plenty of similar examples if you search "join awk" on this site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.