制表符空格用sed或tr替换为“ - ”

时间:2015-08-27 13:23:06

标签: linux bash sed tr

这是我的档案: myfile.txt:

John Jony                                                   Old Boy
Maria                                                       Beautiful Girl
Dede YoYo                                                   Animal

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

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

但我不想替换第二个“标签”空间。 我只是把第一组和第二组用“空间减去空格(” - “)之间。 谢谢你!

1 个答案:

答案 0 :(得分:1)

如果您有更多艺术家喜欢

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  
相关问题