UNIX – Replacing variables in sql with matching values from .profile file

I am trying to write a shell which will take an SQL file as input. Example SQL file:

SELECT *
    FROM %%DB.TBL_%%TBLEXT
  WHERE CITY = '%%CITY'

Now the script should extract all variables, which in this case everything starting with %%. So the output file will be something as below:

%%DB
%%TBLEXT
%%CITY

Now I should be able to extract the matching values from the user’s .profile file for these variables and create the SQL file with the proper values.

SELECT *
    FROM tempdb.TBL_abc
  WHERE CITY = 'Chicago'

As of now I am trying to generate the file1 which will contain all the variables. Below code sample –

sed "s/[(),']//g" "T:/work/shell/sqlfile1.sql" | awk '/%%/{print $NF}' | awk '/%%/{print $NF}' > sqltemp2.sql

takes me till

%%DB.TBL_%%TBLEXT
%%CITY

Can someone help me in getting to file1 listing the variables?


Source: shell

Leave a Reply