Text-Formatting

使用各種複制粘貼重新格式化表格

  • October 28, 2018

我在一個文件中有這樣的東西:

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中做到這一點?

嘗試

awk -vOFS=, '/^[0-9]* / {$1 = $1; GL = $0; next} {print $0, GL}' file
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 ,

引用自:https://unix.stackexchange.com/questions/477562