Reading Multi Line SQL in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading Multi Line SQL in UNIX
# 1  
Old 12-01-2015
Reading Multi Line SQL in UNIX

Hello,

Currently, I am reading few queries from the below .sql file

Code:
--SOURCE TABLE NAME
--SOURCE QUERY
SEL COL1, COL2, COL3, COL4, 
COL5, COL6, COL7 WHERE COL5 = '2015-11-04 16:24:00.000000' FROM SOURCE TABLE;

--TARGET TABLE NAME
--TARGET QUERY
SEL COLUMN1, COLUMN2, COLUMN3, COLUMN4, 
COLUMN5, COLUMN6, COLUMN7 FROM TARGET TABLE;
0,1

The code used to read the contents of the .sql file is being displayed below:

Code:
validate() {
    queryNum=0
    while true
    do
        ((queryNum++))
        read tableName
        read comment
        read sourceQuery 
        read blankLine
        read tableName
        read comment
        read targetQuery 
        read primaryKeyCols || break
        read blankLine
        exQuery "$sourceQuery" sourceResults.txt 
        exQuery "$targetQuery" targetResults.txt         
    done < $1
}

I am using Bash. The only disadvantage with this approach is, I can't read the SQL query if it is written in multi lines. It has to be on a single line to make it work.
I want to be more flexible while reading the queries. Is there any way that I can read the multi line SQL in unix. Please help me out.

Thanks for your help.

Regards,
R

---------- Post updated 12-01-15 at 11:30 AM ---------- Previous update was 11-30-15 at 01:28 PM ----------

---------- Post updated at 12:34 PM ---------- Previous update was at 11:30 AM ----------

Any insights on this?

Last edited by ronitreddy; 12-01-2015 at 12:34 PM..
# 2  
Old 12-01-2015
Very difficult parse. Are you sure there can be some kind of arbitrary 0,1 on a line like you have at the end of your sample? It would be easy if all non-sql lines were either empty or contained comments.... but that 0,1... what's up with that?
# 3  
Old 12-01-2015
Quote:
Originally Posted by cjcox
Very difficult parse. Are you sure there can be some kind of arbitrary 0,1 on a line like you have at the end of your sample? It would be easy if all non-sql lines were either empty or contained comments.... but that 0,1... what's up with that?
Well, those are Primary Key columns.
Those are mandatory without those 0,1..I wont be able to compare source and the target columns..But I can try putting them in comments..
# 4  
Old 12-02-2015
What if after your "read SourceKey"
you check to see if the last character is a comma
then, if so, do a "read SourceKey2" & combine the two

Repeat for the other key field
# 5  
Old 12-02-2015
Alternatively, if you want to find the queries, knowing the allowed query keywords (e.g. SEL, SELE, SELEC, SELECT if all are possible and any other valid SQL keywords)... this would also make the parse pretty trivial.

I'd read the whole line and see if it begins a query (however you want to do that)... then set a flag to be in a query... and look for line (could be same line) ending in semi-colon to terminate the query. Skipping lines that can't be a query.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Slack message multi line from UNIX script

Hi OS: Redhat Version 7.5 Enterprise Trying to post message from shell script to Slack channel and trying below code: text="$msg" text1="$lmsg" if ] then echo "No text specified" exit 1 fi escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )... (13 Replies)
Discussion started by: onenessboy
13 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. Shell Programming and Scripting

Sql multi line comment /* shell interpretation issue

Greetings Experts, I do have some basic knowledge of Unix. The task I am trying to do through shell script is to generate the view script for all of the tables which is in YYYYMMDD format (I assume I am on Ksh). I have certain tables that ends in YYYYMMDD format (eg: tbl_20150630) For each... (1 Reply)
Discussion started by: chill3chee
1 Replies

4. UNIX for Dummies Questions & Answers

UNIX Help for reading line

Hi there, I am trying to read the content such that if there is 'adsmsext.dll', it will read everything from line one until it reads line 6 with 'C:' then it will terminate at line 5. 1. C:\WINDOWS\system32\adsmsext.dll NT AUTHORITY\Authenticated Users:R 2. ... (5 Replies)
Discussion started by: alvinoo
5 Replies

5. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in other.help me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

6. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

Reading line1 with line2 and line 2 with line 3 in unix

Hi, I have a requirement where i have to read a file line by line and see if the string(fixed postion 10 to 15 in the file) in line 2 is greater than string in line 1. I have used following code while read LINE1 do current_inv_no=$(echo "$LINE1" | cut -c 10-15) read... (2 Replies)
Discussion started by: chethanbg2010
2 Replies

8. Shell Programming and Scripting

Reading line by line from unix script

Hi I am a complete newbie in unix. Learning the ropes. I have a task where I have to write a shell script to read a file line by line. I have tried some code from the net like. The file data looks like this. FIN427;2010003;2010003 FIN475;2010001;2010003 FIN476;2010001;2010003... (4 Replies)
Discussion started by: chamajid
4 Replies

9. Shell Programming and Scripting

need help in reading a output of a sql query in unix script

i'm used a sql query in a unix script to get the information from table. but unable to extract the output which i need. Any help with logic will be greatly appreciated. my sql query provide output some thing like this - col1 col2 count ---- ---- ------ A B 10 c D 6 e... (8 Replies)
Discussion started by: pharos467
8 Replies

10. UNIX for Advanced & Expert Users

Reading Particular Line From A File in Unix

Can any u pls tell me :- I want to read a particular line from a file, say line no. 10. This can be done by using awk..but that method I think, is inefficient. Is there any direct command for this? Thanks (2 Replies)
Discussion started by: yeheyaansari
2 Replies
Login or Register to Ask a Question