Skip to main content
added 232 characters in body
Source Link
manatwork
  • 32.1k
  • 8
  • 104
  • 93

The awk solution is much longer, but is easier to make it generic:

awk -F\; '{for(i=1;i<NF;i++)printf"%s;%s",$i,(i>=2)?"NEW;":"";print$NF}' replacefile 

Is possible to do it with sed too, making a loop with the t command and always replacing the 2nd (or whichever you want) separator into some temporary mark (usually \n):

sed ':b;s/;/\n/2;tb;s/\n/;NEW;/g' replacefile 

The awk solution is much longer, but is easier to make it generic:

awk -F\; '{for(i=1;i<NF;i++)printf"%s;%s",$i,(i>=2)?"NEW;":"";print$NF}' replacefile 

The awk solution is much longer, but is easier to make it generic:

awk -F\; '{for(i=1;i<NF;i++)printf"%s;%s",$i,(i>=2)?"NEW;":"";print$NF}' replacefile 

Is possible to do it with sed too, making a loop with the t command and always replacing the 2nd (or whichever you want) separator into some temporary mark (usually \n):

sed ':b;s/;/\n/2;tb;s/\n/;NEW;/g' replacefile 
Source Link
manatwork
  • 32.1k
  • 8
  • 104
  • 93

The awk solution is much longer, but is easier to make it generic:

awk -F\; '{for(i=1;i<NF;i++)printf"%s;%s",$i,(i>=2)?"NEW;":"";print$NF}' replacefile