Skip to main content
3 of 4
corrected correction (supposed to be <= )

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) { line = substr($0,0,ix) ";NEW;" substr($0,ix,length($0)) gsub(";;",";",line) gsub(";$","",line) print line } else {print}} 

Example

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