# replace the column values.

**URL:** <https://community.unix.com/t/replace-the-column-values/191069>\
**Category:** Shell Programming and Scripting\
**Created:** [March 31, 2008, 7:39am UTC](https://community.unix.com/t/replace-the-column-values/191069 "2008-03-31T07:39:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![charandevu](https://community.unix.com/letter_avatar/charandevu/32/6_449c533f586939b6e5f246632a46421f.png) [@charandevu](https://community.unix.com/u/charandevu)\
**Post date:** [March 31, 2008, 7:39am UTC](https://community.unix.com/t/replace-the-column-values/191069/1 "2008-03-31T07:39:22Z")

</div>

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 .....

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [March 31, 2008, 8:17am UTC](https://community.unix.com/t/replace-the-column-values/191069/2 "2008-03-31T08:17:44Z")

</div>

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

```

jean-Pierre.

---

<div class="post-metadata">

**Author:** ![invinzin21](https://community.unix.com/letter_avatar/invinzin21/32/6_449c533f586939b6e5f246632a46421f.png) [@invinzin21](https://community.unix.com/u/invinzin21)\
**Post date:** [April 1, 2008, 4:11am UTC](https://community.unix.com/t/replace-the-column-values/191069/3 "2008-04-01T04:11:03Z")

</div>

Hey, if the figures is constant you can use this 🙂

$ 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

$

---

<div class="post-metadata">

**Author:** ![charandevu](https://community.unix.com/letter_avatar/charandevu/32/6_449c533f586939b6e5f246632a46421f.png) [@charandevu](https://community.unix.com/u/charandevu)\
**Post date:** [April 1, 2008, 7:32am UTC](https://community.unix.com/t/replace-the-column-values/191069/4 "2008-04-01T07:32:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![charandevu](https://community.unix.com/letter_avatar/charandevu/32/6_449c533f586939b6e5f246632a46421f.png) [@charandevu](https://community.unix.com/u/charandevu)\
**Post date:** [April 1, 2008, 8:28am UTC](https://community.unix.com/t/replace-the-column-values/191069/5 "2008-04-01T08:28:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [April 1, 2008, 9:12am UTC](https://community.unix.com/t/replace-the-column-values/191069/6 "2008-04-01T09:12:34Z")

</div>

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

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/6_449c533f586939b6e5f246632a46421f.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [April 2, 2008, 2:21am UTC](https://community.unix.com/t/replace-the-column-values/191069/7 "2008-04-02T02:21:12Z")

</div>

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
