使用各种复制粘贴重新格式化表格

使用各种复制粘贴重新格式化表格

我的文件中有这样的内容:

00550 Peptidoglycan biosynthesis (2)
K01000
K02563
00511 Other glycan degradation (6)
K01190
K01191
K01192
K01201
K01227
K12309  

我需要这样的东西:

K01000,00550,Peptidoglycan biosynthesis (2)
K02563,00550,Peptidoglycan biosynthesis (2)
K01190,00511, Other glycan degradation (6)
K01191,00511, Other glycan degradation (6)
K01192,00511, Other glycan degradation (6)
K01201,00511, Other glycan degradation (6)
K01227,00511, Other glycan degradation (6)
K12309,00511, Other glycan degradation (6)  

我怎样才能在Linux中做到这一点?

答案1

尝试

awk -vOFS=, '/^[0-9]* / {$1 = $1; GL = $0; next} {print $0, GL}' file

答案2

sed '/(/{s/ /,/;h;d;};G;s/\n/,/' filename

表达式sed,一次一条指令:

/(/{
#use "(" to spot a new name
    s/ /,/  #replace first space (after the number) with ,
    h       #save it in the hold space
    d       #delete (don't output) and start next cycle (read next line)
}
#only reached when no "(" found
G           #append saved text from hold to current line
s/\n/,/     #replace the linebreak with a ,

相关内容