私のファイルには次のものがあります。
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
、一度に1つのコマンド:
/(/{
#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 ,