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
bjeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:
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
0
bjeAuthor Commented:
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
0
MikeOM_DBACommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

bjeAuthor Commented:
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
0
MikeOM_DBACommented:
no, try this:

sed "s=\"$value1\"=\"$value2\"=g" < filename.tmp > newfilename
0
TintinCommented:
Here's another approach

#!/bin/bash 
. /temp/table
for value in $(cut -f3 -d, file | sed s/\"//g)
do
  tab=$(eval echo \$table_$value)
  sed -i.bak "s/$value/$tab/" file
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjeAuthor Commented:
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
0
MikeOM_DBACommented:
 sed -i.bak "s/$value/\"$tab\"/" 

Open in new window

:p
0
bjeAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.