一致するものがあれば、file1 の値の一部を file2 の列に追加します。

一致するものがあれば、file1 の値の一部を file2 の列に追加します。

2つのCSVファイルがあります。

ファイル(1列)1:

Prokaryote,Caudovirales,Myoviridae
Prokaryote,Caudovirales,Podoviridae
Prokaryote,Caudovirales,Siphoviridae
Prokaryote,Ligamenvirales,Lipothrixviridae
Prokaryote,Ligamenvirales,Rudiviridae
Prokaryote,Unassigned,Ampullaviridae

およびファイル2(2列):

NC_038375   Baculoviridae,Betabaculovirus,Trichoplusia_ni_granulovirus
NC_000867   Corticoviridae,Corticovirus,Pseudoalteromonas_virus_PM2
NC_000866   Myoviridae,Tequatrovirus,Escherichia_virus_T4
NC_000929   Myoviridae,Muvirus,Escherichia_virus_Mu
NC_004166   Siphoviridae,,Bacillus_phage_SPP1
NC_005859   Siphoviridae,Tequintavirus,Escherichia_virus_T5
NC_002166   Siphoviridae,Hendrixvirus,Escherichia_virus_HK022
NC_008720   Podoviridae,Enquatrovirus,Escherichia_virus_N4
NC_002371   Podoviridae,Lederbergvirus,Salmonella_virus_P22
NC_011048   Podoviridae,Salasvirus,Bacillus_virus_phi29
NNC_001929  Geminiviridae,Begomovirus,Abutilon_mosaic_virus
NC_002649   Podoviridae,Salasvirus,Bacillus_virus_GA1

fnameの3番目の名前に基づいて一致するものがある場合は、2番目の列file 2の各値の先頭にファイル1の名前2を追加したいと思います。例:希望の出力:

NC_038375   Baculoviridae,Betabaculovirus,Trichoplusia_ni_granulovirus
    NC_000867   Corticoviridae,Corticovirus,Pseudoalteromonas_virus_PM2
    NC_000866   Prokaryote,Caudovirales,Myoviridae,Tequatrovirus,Escherichia_virus_T4
    NC_000929   Prokaryote,Caudovirales,Myoviridae,Muvirus,Escherichia_virus_Mu
    NC_004166   Prokaryote,Caudovirales,Siphoviridae,,Bacillus_phage_SPP1
    NC_005859   Prokaryote,Caudovirales,Siphoviridae,Tequintavirus,Escherichia_virus_T5
    NC_002166   Prokaryote,Caudovirales,Siphoviridae,Hendrixvirus,Escherichia_virus_HK022
    NC_008720   Prokaryote,Caudovirales,Podoviridae,Enquatrovirus,Escherichia_virus_N4
    NC_002371   Prokaryote,Caudovirales,Podoviridae,Lederbergvirus,Salmonella_virus_P22
    NC_011048   Prokaryote,Caudovirales,Podoviridae,Salasvirus,Bacillus_virus_phi29
    NNC_001929  Geminiviridae,Begomovirus,Abutilon_mosaic_virus
    NC_002649   Prokaryote,Caudovirales,Podoviridae,Salasvirus,Bacillus_virus_GA1

助けが必要ですか?

答え1

上記の質問に「はい」と答えたと仮定すると、次のようになりますawk

解析.awk

FNR == NR {              # Only for the first file
  h[$3] = $1 "," $2      # Collect column one and two into 'h' hash
  next
}

{ split($2, a, ",") }    # Split the second column of the second file to array 'a'

a[1] in h {              # If the first element of the second column of the 
  $2 = h[a[1]] "," $2    # second file is in 'h' then prepend the value to $2
}

1                        # Print all lines

次のように実行します。

awk -f parse.awk FS=',' file1 FS='\t' OFS='\t' file2

出力:

NC_038375   Baculoviridae,Betabaculovirus,Trichoplusia_ni_granulovirus
NC_000867   Corticoviridae,Corticovirus,Pseudoalteromonas_virus_PM2
NC_000866   Prokaryote,Caudovirales,Myoviridae,Tequatrovirus,Escherichia_virus_T4
NC_000929   Prokaryote,Caudovirales,Myoviridae,Muvirus,Escherichia_virus_Mu
NC_004166   Prokaryote,Caudovirales,Siphoviridae,,Bacillus_phage_SPP1
NC_005859   Prokaryote,Caudovirales,Siphoviridae,Tequintavirus,Escherichia_virus_T5
NC_002166   Prokaryote,Caudovirales,Siphoviridae,Hendrixvirus,Escherichia_virus_HK022
NC_008720   Prokaryote,Caudovirales,Podoviridae,Enquatrovirus,Escherichia_virus_N4
NC_002371   Prokaryote,Caudovirales,Podoviridae,Lederbergvirus,Salmonella_virus_P22
NC_011048   Prokaryote,Caudovirales,Podoviridae,Salasvirus,Bacillus_virus_phi29
NNC_001929  Geminiviridae,Begomovirus,Abutilon_mosaic_virus
NC_002649   Prokaryote,Caudovirales,Podoviridae,Salasvirus,Bacillus_virus_GA1

関連情報