replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC '

NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code.

File 1 :

E100,A456,AAA,E12
E101,A22,BBB,E13
E101,A,AAA,E14

Result file :

E100,A456,CC,E12
E101,A22,BBB,E13
E101,A,CC,E14

Please help .....

awk 'BEGIN { FS=OFS="," } $field==$from { $field=$to } 1' field=3 from=AAA to=CC inputfile

jean-Pierre.

Hey, if the figures is constant you can use this :slight_smile:

$ cat a
E100,A456,AAA,E12
E101,A22,BBB,E13
E101,A,AAA,E14

$ awk -F, '{ if($3 ~ "AAA") {print $1","$2",""CC"","$4} else {print $0} }' a
E100,A456,CC,E12
E101,A22,BBB,E13
E101,A,CC,E14

$

Hi ....
It is working fine ....thanks .....
I will get back if any required in the above .......:slight_smile:

If iam having multiple columns to be replaced ....does the above code works ... please provide the code for the below.....
Below i have to replace 3 rd and 5 th column to 'CC' & 'XXX' repectively .....

File1 :
E100,A456,AAA,E12,MMM
E101,A22,BBB,E13,MMM
E101,A,AAA,E14 ,MMM

Result :

E100,A456,CC,E12,XXX
E101,A22,BBB,E13,XXX
E101,A,CC,E14 ,XXX

awk '
   BEGIN { FS=OFS="," }
   $3 == "AAA" { $3 = "CC" }
   $5 == "MMM" { $5 = "XXX" }
   1
    ' inputfile

Jean-Pierre.

nawk -v p=$1 -v old=$2 -v new=$3 'BEGIN{ OFS=FS=","}
{
for(i=1;i<=NF;i++)
{
if(p==i && $i==old)
{
$i=new
}
}
print $0
}' filename