如果存在匹配,则将 file1 中的部分值添加到文件 2 中的列

如果存在匹配,则将 file1 中的部分值添加到文件 2 中的列

我有 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 中的第三个名称的匹配项,我想在第二列文件 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

相关内容