简体   繁体   中英

tab space replace with “-” with sed or tr

This is my file: myfile.txt :

John Jony                                                   Old Boy
Maria                                                       Beautiful Girl
Dede YoYo                                                   Animal

with : sed 's/ \\+ /\\ - /g' myfile.txt > ttt.txt
I have this output :

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

But I dont want to replace the second "tab" space. I just take the first group and the second group with "space minus space (" - ") between. Thanks you!

If you have more artists like

John Jony    Another Name    Old Boy  

and want this output:

John Jony & Another Name - Old Boy

then it's a little more complicated, but not impossible for awk :

Input file:

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

Command:

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM