Skip to main content
fixed codez
Source Link

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

Example

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

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 

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 
corrected correction (supposed to be <= )
Source Link

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 

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 

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 
corrected wrong HTML entity usage in “>=” operator
Source Link
manatwork
  • 32.1k
  • 8
  • 104
  • 93

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

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 &lt= $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 

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 
Source Link
Loading