ファイルで一致する値を見つけて別のファイルに置き換えるには?

ファイルで一致する値を見つけて別のファイルに置き換えるには?

次のようにスペースで区切られたファイルがあります。 (1775行があります)

head output.fam
0 ALIKE_g_1LTX827_BI_SNP_F01_33250.CEL 0 0 0 -9
0 BURRY_g_3KYJ479_BI_SNP_A12_40182.CEL 0 0 0 -9
0 ABAFT_g_4RWG569_BI_SNP_E12_35136.CEL 0 0 0 -9
0 MILLE_g_5AVC089_BI_SNP_F02_35746.CEL 0 0 0 -9
0 PEDAL_g_8WWR250_BI_SNP_B06_37732.CEL 0 0 0 -9
...

カンマ区切りファイル phg000008.individualinfo(1838行):

#Phen_Sample_ID - individual sample name associated with phenotypes
#Geno_Sample_ID - sample name associates with genotypes
#Ind_id - unique individual name which can be used to match duplicates (in this case same as Phen_Sample_ID)
#Ped_id - Pedigree ID
#Fa_id - Father individual ID
#Ma_id - Mother individual ID
#Sex - coded 1 for Male, 2 for Female
#Ind_QC_flag - value "ALL" indicates released in both Quality Filtered and Complete set
#Genotyping_Plate
#Sample_plate_well_string - This string corresponds to the file within the CEL files distribution
#Genotype_Clustering_Set
#Study-id - dbGaP assigned study id
 #Phen_ID,Geno_Sample_ID,Ind_id,Ped_id,Fa_id,Ma_id,Sex,Ind_QC_flag,Genotyping_Plate,Sample_plate_well_string,Genotyping_Clustering_Set,Study_id
G1000,G1000,G1000,fam1000-,0,0,2,ALL,7FDZ321,POSED_g_7FDZ321_BI_SNP_B02_36506,set05,phs000018
G1001,G1001,G1001,fam1001-,G4243,G4205,1,ALL,3KYJ479,BURRY_g_3KYJ479_BI_SNP_H04_40068,set02,phs000018       
G2208,G2208,G2208,fam2208-,G3119,G3120,2,ALL,1LTX827,ALIKE_g_1LTX827_BI_SNP_F01_33250,set01,phs000018
G1676,G1676,G1676,fam1676-,G1675,G1674,1,ALL,3KYJ479,BURRY_g_3KYJ479_BI_SNP_A12_40182,set02,phs000018
...

Output.famの2番目の列で値が見つかるかどうかを確認し(たとえば、phg000008.individualinfoのALIKE_g_1LTX827_BI_SNP_F01_33250.CEL(.CELサフィックスを無視))、そのエントリがある行があるかどうかを確認して、私のoutput.famを変更します。これを置き換えると、output.famのエントリはphg000008.individualinfoの最初の列の値に置き換えられ、同じ行に対してoutput.famの最初の列の値は次の値に置き換えられます。 phg000008.individualinfoの4番目の列(サフィックスを除く)

たとえば、2行がある場合、output.famは次のようになります。

fam2208 G2208 0 0 0 -9
fam1676 G1676 0 0 0 -9

ベストアンサー1

努力する

awk '
FNR == NR       {sub (/-/, "", $4)              # get rid of "-" in $4
                 T[$10 ".CEL"] = $4 " " $1      # save file2 in temp array
                 next
                }
$2 in T         {$1 = T[$2]                     # check if $2 is relevant; replace 
                 $2 = ""                        # $1 with temp array value; delete $2
                 print  
                }
' FS=, file2 FS=" " file1

おすすめ記事