0

我有一个巨大的文件结构:

>ABC_123|XX|YY|ID
CNHGYDGHA
>BBC_153|XX|YY|ID
ACGFDRER

我需要根据在线的第一个值拆分此文件

File1: ABC_123 -> should contain 
          >ABC_123|XX|YY|ID
          CNHGYDGHA

File2: BBC_153 -> should contain 
         >BBC_153|XX|YY|ID
          ACGFDRER
4

2 回答 2

0

这会产生两个文件ABC_123,并BBC_153根据您的输入:

awk -F'|' 'NF > 1 { # when more than one field (i.e. line contains | )
    close(out)      # close the previous file (or do nothing, if none were open)
    out = $1        # assign first field to filename
    sub(/^>/, "", out) # remove the > from the start of the name
} 
{ print >> out }' file # print to the file, opening in append mode if needed

如果您确定文件名只会打开一次,那么您可以>使用>>.

于 2018-03-19T09:49:59.473 回答
0

awk方法:

awk -F'|' '/^>.+\|/{ fn = substr($1, 2) }{ print > fn }' file

查看 2 个创建的示例文件:

$ head [AB]BC_*
==> ABC_123 <==
>ABC_123|XX|YY|ID
CNHGYDGHA

==> BBC_153 <==
>BBC_153|XX|YY|ID
ACGFDRER
于 2018-03-19T09:53:35.160 回答