Avatar of bje
bje
Flag for United States of America asked on

using a table to replace a value in a file

hello,
have a file that i would like to replace the value in the third position (file is comma separated) with a value in a table.  the value in the table begins with a "  and ends in a ".  the sed command to replace does not work.

numofrows=`wc -l tempfile.tmp`
 for i in $numofrows
   do
    thirdvalue=$(awk -F"," '{print $3}' tempfile.tmp )
     eval tablename=\$table_${thirdvalue}
    sed 's/"$thirdvalue"/"$tablename"/g' < tempfile.tmp > newtempfile.tmp
   done


table is this
table_value1="bb=value12,cc=value13"
table_value2="hh=value24,cc=value13"


at the beginning of the script i have this to determine the server and table to use.

hostname=`uname -n`
case $hostname
in
       servername ).      /temp/tablename.sh;;
esac


Thanks in advance for the help
Shell ScriptingLinux

Avatar of undefined
Last Comment
bje

8/22/2022 - Mon
MikeOM_DBA

Please post sample data for this file:
. . .have a file that i would like to replace the value in the third position (file is comma separated). . .
Also sample data for this table:
. . . with a value in a table. . . .
What do you mean by "determine the server and table to use"? Use for what?
:p
bje

ASKER
If the server is a test server or prodution server the values in tablename.sh would be different.  the same script handles both test and production servers.

i use the word table for  tablename.sh to indicate the values in this file are for lookup and replace purpose.

the file looks like this,
value1,value2,value3,value4,value5

the tablename.sh looks like this,
table_dog="bb=value12,cc=value13"
table_cat="hh=value24,cc=value13"

if value3 in the file equals dog, then replace with the value with the information in tablename.sh  "bb=value12,cc=value13"

so the new file would be
value1,value2,"bb=value12,cc=value13",value4,value5


thanks
MikeOM_DBA

Try something like this:
#Source table (replace.tmp):
cat - <<! >replace.tmp
table_dog="bb=value12,cc=value13"
table_cat="hh=value24,cc=value13"
!
#Target table (tempfile.tmp):
cat - <<! >tempfile.tmp
aa1,bb2,"dog",1234
aa2,bb3,"cat",5678
!
awk -F',' 'BEGIN{OFS=",";
        while ((getline line < "replace.tmp") > 0)
              {n0=index(line,"=");split(substr(line,1,n0-1),t,"_");
               v0[++i,1]=t[2];v0[i,2]=substr(line,n0+1);
        }
        n9=i;
        close("replace.tmp");}
{ 
  for (y=1;y<=NF;y++) o[y]=$y;
  for (x=1;x<=n9;x++) {o3=substr(o[3],2,length(o[3])-2);
    if (o3 == v0[x,1]) o[3]=v0[x,2];}
  for (y=1;y<=NF;++y) printf "%s,",o[y];
  printf "\n"
}' tempfile.tmp > newtempfile.tmp
$ cat newtempfile.tmp
aa1,bb2,"bb=value12,cc=value13",1234,
aa2,bb3,"hh=value24,cc=value13",5678,

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
bje

ASKER
Thanks, will give this a try.

I am working on another task and using sed to replace values that are stored in a variable.

This is what i have,

sed 's/"$value1"/"$value2"/g' < filename.tmp > newfilename

is this correct?

Thanks
MikeOM_DBA

no, try this:

sed "s=\"$value1\"=\"$value2\"=g" < filename.tmp > newfilename
ASKER CERTIFIED SOLUTION
Tintin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bje

ASKER
Tintin,
The solution worked well.   My table has double quotes around the value, however they are not showing up in the file.

I tried, sed -i.bak "s/$value/\"/$tab\"\/"  -  not working.   Will you let me know how to do this.

Thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
MikeOM_DBA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bje

ASKER
Thank you