The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script for sql queries user71408 UNIX for Dummies Questions & Answers 1 05-21-2008 05:31 AM
script not working...select utility mobydick Shell Programming and Scripting 6 05-08-2008 06:33 AM
Extracting Table names from a Select Stament silas.john Shell Programming and Scripting 2 02-29-2008 07:46 AM
Sh Shell Script executing remote SQL queries Javed UNIX for Dummies Questions & Answers 2 07-24-2006 04:46 AM
shell script queries: $home; broadcast ping bionicfysh Shell Programming and Scripting 6 08-30-2002 09:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-10-2008
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Question Extracting select queries from script

Hi,

I have a script in which a lot of sql queries are embedded ie,"Select .....;".My purpose is to document all the dml statements in the script along with the line number.

I am thinking of writing a perlscript or by using awk/sed scripts for extracting all the 'select' statements/queries along with the line number.
I need the entire query,ie starting from Select to semicolon(.

Can anyone please help me to achieve this.Your help is appreciated
  #2 (permalink)  
Old 04-10-2008
helper helper is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 25
Arrow

Hey, a sample file would help me out in getting what u want.
The reason for this is if a its a normal select statment then it completes in one line.
If the SQL statement has sub-queries which spans more that one line then we would have to concatenate it so that it becomes one line and that can be presented ....


Let us know regarding this.
  #3 (permalink)  
Old 04-10-2008
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Post

Quote:
Originally Posted by helper View Post
Hey, a sample file would help me out in getting what u want.
The reason for this is if a its a normal select statment then it completes in one line.
If the SQL statement has sub-queries which spans more that one line then we would have to concatenate it so that it becomes one line and that can be presented ....


Let us know regarding this.
Thanks for the reply.
The file contains select statements which spans over multiple lines.I can say the max number of lines as 10.

Regards
Dileep
  #4 (permalink)  
Old 04-10-2008
helper helper is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 25
Arrow Use SED command to do the need.

First of all i dont think u need any scripts..
U can do with a single line command along with pipes
Here is the command

sed '/;/G' test | grep "SELECT" | sed -e :a -e '$!N;s/\n/~/g;ta' | tr "~" "\n" >> targetfile

INPUT FILE : test
OUTPUT FILE : targetfile

Assumptions :
*************
1. The input file doesnt have any blank lines.
2. If it has then use the following sed command
sed '/^$/d' <sourcefile> <targetfile>

Here is my input file
cat test
1 SELECT * FROM DUAL;
2 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I ;
3 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H
I A B C D IN ( SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I );
4 ASDFASDFASDFASDFSAD
5 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B
I A B C D IN ( SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I );

THINGS THAT ARE TAKEN CARE OFF :
*********************************
1. There might be a single SELECT STATEMENT (no sub-queries).
2. There might be sub-queries which are written continiously (without breaking)
3. There might be sub-queries which are written in different statements.


Now the command :
sed '/;/G' test | grep "SELECT" | sed -e :a -e '$!N;s/\n/~/g;ta' | tr "~" "\n" >> targetfile

Explanation of the commands :
*****************
***sed '/;/G' test ***
*****************

This inserts a new line after the end of a complete SQL statment ie. when it encounters a ";" it inserts a line after it.

*****************
***grep "SELECT" ***
*****************
The output of the command sed '/;/G' test is piped and only the SELECT queries are grepped from that.

********************************
****sed -e :a -e '$!N;s/\n/~/g;ta'******
********************************

The output of the previous command sed '/;/G' test | grep "SELECT" is sent as input to the above command. This is manily done becoz if there are queries which span more than one line which is not continuous. (they might have pressed enter key and continued)

**************
***tr "~" "\n"***
**************
This will replace all "~" characters to "\n"
sed '/;/G' test | grep "SELECT" | sed -e :a -e '$!N;s/\n/~/g;ta' | tr "~" "\n"
1 SELECT * FROM DUAL;
2 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I ;
3 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H
I A B C D IN ( SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I );
5 SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I A B C D E F G H I A B
I A B C D IN ( SELECT A B C D E F G H I A B C D E F G H IA B C D E F G H I A B C D E F G H I );

Here is the file which u needed....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0