To extract query from a text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To extract query from a text file.
# 1  
Old 09-28-2016
To extract query from a text file.

Hi,

I've a txt file with the following contents:

Code:
variable = "select name, emp_id, org_name "
variable = variable + " from table_name where "
variable = variable + " condition_1 = ABC and "
variable = variable + " condition_2 = DEF "
varaible = variable + " order by other condition "

now this variable will be having a query which will be fed to custom work engine which actually takes up the whole message and runs.

Now here, I would like derive the sql query alone and put it to some fine tuning measures to check if this query is proper..

Can anyone help on how to get the query alone from here.

Thansk,
Arjun


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-28-2016 at 05:17 AM.. Reason: Added CODE tags.
# 2  
Old 09-28-2016
There's way more info necessary to answer your question. Is that a shell script that you want to analyse (syntax indicates it is not)? What tools are you going to use? Why don't you just print the variable's contents to do your "fine tuning"?
# 3  
Old 09-28-2016
To extract query from a text file.

I would like to use shell script here.
The problem is the variable is defined in a proprietary language and in a proprietary file and the name of the variable might not be the same every time. so we would need a pattern from which i would need to decipher the value.

the idea is to feed the txt file which would be analyzed to get the query. everytime the name of the variable might not be the same and the shell would have no idea what the name of the variable is.. the shell can only recognize the pattern and extract the query as such.
# 4  
Old 09-28-2016
Try
Code:
while IFS='"' read A B; do C=$C$B; done < file; echo $C
select name, emp_id, org_name from table_name where condition_1 = ABC and condition_2 = DEF order by other condition

You are aware of some syntactical problems with the resulting line?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-28-2016
Hi,
thanks alot for the lead. I believe this can provide a solution for my issue. can you also let me know how this works? that would also help me understand the syntax so that i can tweak this in case a change is required.

Thanks,
Arjun
# 6  
Old 09-28-2016
In a while loop, the input file is read (stdin redirection) with a redefined "Internal Field Separator" (set to " ). As there are two IFS chars in every line, we have three fields (the third being empty), of which we accumulate the second into the C variable.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-28-2016
ok yes .. got it.
this would work for the given example.. and if in the due future, if I add some normal execution code after the variable set and then define another sql query in another variable say variable2, will this generate the whole sql queries as a single line or two separate query lines.

eg:

Code:
variable = "select name, emp_id, org_name "
variable = variable + " from table_name where "
variable = variable + " condition_1 = ABC and "
variable = variable + " condition_2 = DEF "
varaible = variable + " order by other condition "

Other code

Code:
variable2 = "select dept_id, dept_name "
variable2 = variable2 + "from dept_table "
variable2 = varaible2 + "where condition_3 = IJK "

In this case, is there any possible way to get the 2 queries in two separate lines or will I be getting it as a single line.

Thanks,
Arjun

Last edited by Don Cragun; 09-28-2016 at 08:42 AM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write MySQL query in text file and run in Linux?

I would like to call a .sql file from a .sh file in linux. The .sql file will contain queries to MySQL database. I am able to call the .sql file by running .sh file in linux, but the sql query is not working. Below is my syntax. The select statement is throwing below error. ./sample.sql: line... (1 Reply)
Discussion started by: qytan
1 Replies

2. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

3. Shell Programming and Scripting

Get extract text from xml file

Hi Collegue, i have a file say a.xml. it has contents <bpelFault><faultType>1</faultType><genericSystemFault xmlns=""><part name="payload"><v2:Fault... (10 Replies)
Discussion started by: Jewel
10 Replies

4. Shell Programming and Scripting

Small query on modifying the text of file

Hi, I have file with the following text, /analysis2012/533/mc_S10_533/DYJets50/vgtree_1_1_33l.root /analysis2012/533/mc_S10_533/DYJets50/vgtree_2_5_33l.root /analysis2012/533/mc_S10_533/DYJets50/vgtree_6_7_33l.root and many such files. For some operation on these files, I would like... (9 Replies)
Discussion started by: emily
9 Replies

5. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

6. Shell Programming and Scripting

extract text from a file

I have been reading several posts regarding how to extract text from a file, but none of those have helped me for what I need. This is my problem: I need to extract the text after my pattern So my line is: 485.74 6589.5 Log likelihood: 1485.79 My pattern is 'Log likelihood:' and I need... (2 Replies)
Discussion started by: loperam
2 Replies

7. Solaris

Mysql query reading a text file.

Hi, Is there any way that mysql query reads the content from a text file which has data in the below format: 1,2,3,4,5 and selects matched data from another table. ie I have a table named xyz which has same ids as in that file. I want this query to get count of the ids from xyz file by... (6 Replies)
Discussion started by: jyothi_wipro
6 Replies

8. Shell Programming and Scripting

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

9. Shell Programming and Scripting

shellscript.query Oracle table..populate in a text file

Hi Guys, I'm new to this forum as well as to UNIX shell scripting. I'm looking for a shellscript to query an Oracle database table and populate the result set of the query in a text file. Could you someone help me out with a sample code? Thanks, Bhagat (7 Replies)
Discussion started by: bhagat.singh-j
7 Replies

10. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies
Login or Register to Ask a Question