How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below

# For Comma seperated file without quotes

 awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName}      
 

# For Comma seperated file with quotes

awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="\""'"$PPK"'"\""{$"'"$ColumnNo"'"="\""'"$NPK"'"\""}{print}' ${FileName}   

But I wanted to make it in one command so that it works for both type of file. I know I can do with sed command sed -i "s/$PPK/$NPK/" $FileName but it replaces the value in other columns as well so can anyone suggest what can be done ?

Here ColumnNo is the nth column from which I wanted to replace PPK to NPK.

Please find other details

 uname -a
Linux XXXXXXX 2.6.32-220.25.1.el6.x86_64 #1 SMP Tue Aug 14 13:14:38 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
echo $SHELL
/bin/ksh
echo $KSH_VERSION
Version JM 93t+ 2010-06-21

This description is a bit diffuse. Try

awk -v COL=$ColumnNo -v SRCH="$PPK" -v REP="$NPK" '
        {FS=OFS="\""; $0=$0                                     # recalculate fields based on " delimiter
         for (i=2; i<=NF; i+=2) gsub (SEP,"\001", $i)           # replace , in " delimited fields with a token
         FS=OFS=SEP; $0=$0                                      # recalculate fields based on , delimiter

         sub (SRCH, REP, $COL)                                  # do whatever you need 

         gsub ("\001", SEP)                                     # just before printing anything, put back the inner ","
        }
1 
' SEP="," file

This is untested as a sample is missing.

1 Like

Thanks RudiC, your solution worked for me and I have learnt 2 new function sub & gsub but I simplified it as per below command

echo '"Amit","Amit"' | awk  -v COL=2 -v SRCH="Amit" -v REP="XXX" 'BEGIN{OFS=FS="," } {sub (SRCH, REP, $COL);print }'
echo 'Amit,Amit'     | awk  -v COL=2 -v SRCH="Amit" -v REP="XXX" 'BEGIN{OFS=FS="," } {sub (SRCH, REP, $COL);print }'
 

It is also working fine. Can you please suggest, Is there any limitation or drawback from my commands or it will work perfectly ?

Hi Amit,
what does awk -v stands for it.?

More like:

awk  -v COL=2

-v requires a variable name of your choosing and an assignment.

Man pages for awk in Linux (gawk)

1 Like

Amit, you can simplify as you did unless there are fields in quotes that have commas in them.