Visit Our UNIX and Linux User Community


Replace a var in sql file with shell script variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a var in sql file with shell script variable
# 1  
Old 09-29-2010
Replace a var in sql file with shell script variable

I have a requirement where i have a sql file (filetext.sql). This file contains a variable ss_code.
Now in a shell script im trying to replace the variable ss_code with a value contained in the shell script variable MTK_DC..tried the below in the script

Code:
MTK_DC="mit,cit,bit"
OUT=`awk -v ss_code=$MTK_DC '{print}' filetext.sql` # prints as such the sql file .. without replacing the variable

Code:
MTK_DC="mit,cit,bit"
OUT=`awk -v bb_code=$MTK_DC '{gsub(ss_code, "bb_code")}1' filetext.sql` # this gives undesired output

Kindly throw some light..

Pls note:sql file and the shell script file are two different files present in the same location
# 2  
Old 09-29-2010
Post the sql file and the desired ouput.
# 3  
Old 09-29-2010
This is my sql file:
Quote:
select cr.dol,cr.unit from cardb cr
where cr.frm in (ss_code)
after running the below script

Code:
MTK_DC="'mit','cit','bit'"
OUT=`awk -v bb_code=$MTK_DC '{gsub(ss_code, "bb_code")}1' filetext.sql` # this gives undesired output

the sql file should change to
Quote:
select cr.dol,cr.unit from cardb cr
where cr.frm in ('mit','cit','bit')
i have tried and got the desired o/p by changing the script as below. Pls tell me if the same could be got thru sed command
Code:
MTK_DC="'mit','cit','bit'"
OUT=`awk -v bb_code=$MTK_DC '{gsub("ss_code", bb_code)}1' filetext.sql` # this gives undesired output

# 4  
Old 10-01-2010
Try this:
Code:
MTK_DC="'mit','cit','bit'"
awk -v var=$MTK_DC '/ss_code/{sub("ss_code", var)}1' filetext.sql

# 5  
Old 10-01-2010
Thanks.. it worked out..

Previous Thread | Next Thread
Test Your Knowledge in Computers #587
Difficulty: Medium
All programming languages support recursion.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to call sql file

hi , the below script contains sql query and after executed it sends the output of the query (output.txt) to an email body with conditional subject line based on the output of all_counts_match.txt. i want to make this script generic so that it can accept the sql file as parameter and can... (5 Replies)
Discussion started by: itzkashi
5 Replies

2. Shell Programming and Scripting

Passing variable from file to sql from script

Hi Friend, I have one file in which some number are mentioned and number of lines are vary every time And i need to pass that number to my sql command from script. Suppose i have file acc.txt 45456546456 45464564565 67854353454 67657612132 Number of records are vary every time.... (20 Replies)
Discussion started by: pallvi_mahajan
20 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Help with a shell script to check /var file system

deleting (0 Replies)
Discussion started by: fretagi
0 Replies

5. UNIX for Dummies Questions & Answers

SQL Script to use variable value from input file

Here is the requirement, When I run the "run file KSH (sql)", it should substitute '${pCW_Bgn_DT}' with 201120 and '${pCW_End_DT}' with 201124 Input File ---------- $ cat prevwk.dat 201124 20110711 run file KSH (sql) ------------------ In this file, I want to use the variables... (1 Reply)
Discussion started by: shanrice
1 Replies

6. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

7. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

9. UNIX for Advanced & Expert Users

Calling sql file from shell script

Hi I have a shell script that call a sql file. The sql file will create a spool file. My requirement is, when ever i get an OS error like file not found. I have to log it in a log file. Could some who worked in a like scenario help me by giving the code sample. Many Thanks.. (1 Reply)
Discussion started by: chintapalli001
1 Replies

10. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies

Featured Tech Videos