Skip to main content
4 of 4
fixed codez

I can do it with more code but no loops!

Data

china;US;nigeria;mexico;russia iindia;austria;japan;chile 

Script

BEGIN{ FS=";" }{ insert=$param ix=index($0, insert) + length(insert) if (NF <= $param) { rest = substr($0,ix,length($0)) gsub(";",";NEW;",rest) line = substr($0,0,ix) rest gsub(";;",";",line) gsub(";$","",line) print line } else {print}} 

Example

 Microknoppix v # awk -f replaceNth.awk -v param=2 countries china;US;NEW;nigeria;NEW;mexico;NEW;russia iindia;austria;NEW;japan;NEW;chile