除最后一列外,用 TAB 替换空格

除最后一列外,用 TAB 替换空格

我有一个看起来像这样的文件。除了以 ID 开头的最后一列之外,如何用 TAB 替换每个空格?

chr1 13513 14763 Medtr1g004950 . + JCVI gene . ID=Medtr1g004950;Note=hypothetical protein
chr1 15282 16532 Medtr1g004960 . + JCVI gene . ID=Medtr1g004960;Note=hypothetical protein
chr1 30972 32222 Medtr1g004980 . + JCVI gene . ID=Medtr1g004980;Note=hypothetical protein

答案1

任君选择:

$ sed 's/ /\t/g; s/\t/ /10' file
chr1    13513   14763   Medtr1g004950   .       +       JCVI    gene    .       ID=Medtr1g004950;Note=hypothetical protein
chr1    15282   16532   Medtr1g004960   .       +       JCVI    gene    .       ID=Medtr1g004960;Note=hypothetical protein
chr1    30972   32222   Medtr1g004980   .       +       JCVI    gene    .       ID=Medtr1g004980;Note=hypothetical protein

$ sed 's/ /\t/g; s/\t\([^\t]*\)$/ \1/' file
chr1    13513   14763   Medtr1g004950   .       +       JCVI    gene    .       ID=Medtr1g004950;Note=hypothetical protein
chr1    15282   16532   Medtr1g004960   .       +       JCVI    gene    .       ID=Medtr1g004960;Note=hypothetical protein
chr1    30972   32222   Medtr1g004980   .       +       JCVI    gene    .       ID=Medtr1g004980;Note=hypothetical protein

答案2

awk '{l=$NF;$NF="";gsub(" ","\t",$0);gsub(/[[:space:]]$/, "", $0);print $0,l}'  filename


chr1    13513   14763   Medtr1g004950   .       +       JCVI    gene    .       ID=Medtr1g004950;Note=hypothetical protein
chr1    15282   16532   Medtr1g004960   .       +       JCVI    gene    .       ID=Medtr1g004960;Note=hypothetical protein
chr1    30972   32222   Medtr1g004980   .       +       JCVI    gene    .       ID=Medtr1g004980;Note=hypothetical protein

相关内容