Snowman
Snowman

Reputation: 73

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!

Upvotes: 3

Views: 264

Answers (1)

MichalH
MichalH

Reputation: 1074

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  

Upvotes: 1

Related Questions