Results 1 to 4 of 4
Greetings,
I am working on a menu driven bash script.
I need to import text for sql queries into the main script and modify some variable data (dates) to pull ...
- 06-13-2008 #1Just Joined!
- Join Date
- Jun 2008
- Location
- Grand Rapids, MI
- Posts
- 2
Inserting variables into imported text
Greetings,
I am working on a menu driven bash script.
I need to import text for sql queries into the main script and modify some variable data (dates) to pull the information I want.
I get the date form a user input read prompt. I can pass the variable around.
But I need to know how to use that variable inside the imported text.
I am pulling the text in using the following:
while read LINE
do
echo $LINE
done < filename
Here is an example of one of the scripts.
select stno, cusid, invno, adjamt from invheader
where trantype='A' AND VOID=’N’ and invdate>'X/26/2008’
I want to replace the invdate with a variable date.
Suggestions?
Thanks
Larry
- 06-13-2008 #2
if you tell us how you are going to import the sql scripts and what do you do with them afterwards it would help us. are the sql scripts in a file? do you write back the result after the change to the file?
Linux and me it's a love story
- 06-13-2008 #3Just Joined!
- Join Date
- Jun 2008
- Location
- Grand Rapids, MI
- Posts
- 2
The sql are in files that I want to load and run in memory. I have about 130 different statements I run every month... takes all day to do it by cut-and-paste.
I could rewrite them back to text files and have another function call that rewritten file... seems wasteful.
I have a group of variables:
$prev_date
$curr_date
$curr_month
$fin_stmt_date
I would like to take the sql statement..
select stno, cusid, invno, adjamt from invheader where trantype='A' AND VOID=’N’ and invdate>'X/26/2008’
and convert it to this and run it in one process..
select stno, cusid, invno, adjamt from invheader where trantype='A' AND VOID=’N’ and invdate>$prev_date
I already do some minor checking using isqf-fb to connect to our database and pull info.
I just need to know how to pull a test file in, manipulate it and use it. The date are not always in the same place in the sql queries.
Hope this helps
- 06-15-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
If you use "invdate" as a placeholder for $curr_date (or any other variable, it's just an example), you could just use sed to make string substitution. Or, you could as well use the bash string mangling capabilities, examples:
To substitute all the ocurrences in the file. Or this:Code:sed -e 's/invdate/'$curr_date'/g' filename
Which in bash could also be done:Code:LINE=$(echo "$LINE | sed -e 's/invdate/'$curr_date'/'
This form is much cleaner, but, as said, this will only work on bash, while the sed solution is portable and should work regardless of the shell you use.Code:LINE=${LINE/invdate/$curr_date}


Reply With Quote