Using awk to replace strings

Hi.. I have a file that has the following content :

abc 213 24 213
pqr 456#34 678
xyz 213 45%213

i need to write an awk script that will replace the second 213 in all the lines, if it is present. The IFS can not be specified and can be random.

The number of lines in the file and the columns in each line are not fixed.

Please suggest a solution. Thanks!

Hi,
Use code tag for input file...
You want replace the second 213 but with what ?
to do this, it's easiest with sed:

sed -e 's/[^0-9]213[^0-9]/value_replacement/2' inputfile

Regards.

i need to replace it with 243. Also, it is a part of an awk script so need to use awk alone. Need some generalized script to specify the substring at a certain position alone in a line.

It's really no easy with awk, but if you use gawk, you can do it with:

 gawk '{A=gensub(/([^0-9])123([^0-9])/,"\\1\n123\n\\2",$0); A=gensub(/([^\n0-9])123([^\n0-9])/,"\\1243\\2",A); gsub(/\n/,"",A); print A}'

Regards.

How about

awk '{sub (/213/, "\001");sub (/213/,"243"); sub (/\001/, "213")} 1' file3
abc 213 24 243
pqr 456#34 678
xyz 213 45%243

Please provide a desired output sample.