Link to home
Create AccountLog in
Avatar of bje
bjeFlag 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
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

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
Avatar of 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
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

Avatar of 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
no, try this:

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

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of bje

ASKER

Thank you