![除最后一列外,用 TAB 替换空格](https://linux22.com/image/202084/%E9%99%A4%E6%9C%80%E5%90%8E%E4%B8%80%E5%88%97%E5%A4%96%EF%BC%8C%E7%94%A8%20TAB%20%E6%9B%BF%E6%8D%A2%E7%A9%BA%E6%A0%BC.png)
我有一个看起来像这样的文件。除了以 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