使用awk或sed分割字段

时间:2013-05-15 07:33:13

标签: sed awk split

我有一个带有列的文件,如下所示,

 chr1 412573  .   A   C 2754.00      .  AC=2;AF=1.00;AN=2;DP=59;Dels=0.00;FS=0.000;HaplotypeScore=0.0000;MLEAC=2;MLEAF=1.00;MQ=59.22;M  
 Q0=0;QD=35.08;SB=-5.570e+02;resource.EFF=NON_SYNONYMOUS_CODING(MODERATE|MISSENSE|gCg/gTg|A124V
|RBFA|protein_coding|CODING|ENSCAFT00000037436|5)

我想在第8列中拆分和提取数据,如下所示,

chr1 412573  .   A   C 2754.00      .  59    59.22    NON_SYNONYMOUS_CODING    MODERATE    MISSENSE    gCg/gTg    A124V    RBFA   protein_coding    CODING    ENSCAFT00000037436    5

2 个答案:

答案 0 :(得分:1)

awk '/DP=/ || /MQ=/ || /EFF=/ {gsub("[\\|()]", "\t"); print $2}' RS=';' ORS='\t' FS='='

结果

59  59.22   NON_SYNONYMOUS_CODING   MODERATE    MISSENSE    gCg/gTg A124V   RBFA    protein_coding  CODING  ENSCAFT00000037436  5

答案 1 :(得分:0)

使用awk的一种方式:

script.awk的内容:

BEGIN {
        FS = ";"
}

{
        str = $NF
        sub( /^[^=]*=/, "", str )
        gsub( /[|()]/, "\t", str )
        print str
}

像以下一样运行:

awk -f script.awk infile

产量:

NON_SYNONYMOUS_CODING   MODERATE    MISSENSE    gCg/gTg A124V   RBFA    protein_coding  CODING  ENSCAFT00000037436  5