标签空间replace为“ – ”与sed或tr

这是我的文件:myfile.txt:

John Jony Old Boy Maria Beautiful Girl Dede YoYo Animal 

与: sed 's/ \+ /\ - /g' myfile.txt > ttt.txt
我有这个输出:

 John Jony - Old Boy - Maria - Beautiful Girl - Dede YoYo - Animal - 

但我不想取代第二个“标签”空间。 我把第一组和第二组的空间减去空格(“ – ”),谢谢!

如果你有更多的艺术家喜欢

 John Jony Another Name Old Boy 

并希望这个输出:

 John Jony & Another Name - Old Boy 

那么awk有点复杂,但不是不可能的:

输入文件:

 John Jony Another Name Old Boy First artist Second artist Third artist Song Maria Beautiful Girl Dede YoYo Animal 

命令:

 awk -F ' +' ' NF==3 { sub(" +"," - "); print } NF>3 { for (i=1;i<=NF-3;i++) { printf("%s",$i); printf(" & "); } printf("%s - %s\n",$(i++),$i); }' myfile.txt > ttt.txt 

输出:

 John Jony & Another Name - Old Boy First artist & Second artist & Third artist - Song Maria - Beautiful Girl Dede YoYo - Animal