ksh program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh program
# 15  
Old 01-10-2012
Quote:
Originally Posted by manasa_vs
...............
...............
..............................
for example we can consider below statements as the content:-

select some text
(select some text(select some text)
where some condition)
where condition;
some statements some statements some statements;
update some text where condition;
some statements some statements;
delete some text where condition;
update some text condition;
delete some text condition;
...............
..............................
Code:
# cat yourfile
select some text
(select some text(select some text)
where some condition)
where condition;
some statements some statements some statements;
update some text where condition;
some statements some statements;
delete some text where condition;
update some text condition;
delete some text condition;

Code:
# awk '{gsub("\n","");}
{
if(match($0,"[(].*[)]"))
x=$0;while(match(x,"[(].*[)]")){
s=gensub(/.*\(((.[^)]*))\).*/,"\\1",$0);
if(s~/where/){print "["NR"]","is [[SUCCESS]] ->",s}
else{print "["NR"]","is [FALSE] ->",s;}x=gensub("[(]"s"[)]","",x);$0=x}
if($0~/^$/){next}if($0!~/select|delete|update|insert/){print "["NR"]","may not a SQL state ->",$0}
else{
if($0~/where/){print "["NR"]","is [[SUCCESS]] ->",$0;}
else{print "["NR"]","is [FALSE] ->",$0;}}
}' RS=';' yourfile
[1] is [FALSE] -> select some text
[1] is [[SUCCESS]] -> select some text where some condition
[1] is [[SUCCESS]] -> select some text  where condition
[2] may not a SQL state -> some statements some statements some statements
[3] is [[SUCCESS]] -> update some text where condition
[4] may not a SQL state -> some statements some statements
[5] is [[SUCCESS]] -> delete some text where condition
[6] is [FALSE] -> update some text condition
[7] is [FALSE] -> delete some text condition

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 16  
Old 01-10-2012
Nice solution ygemici,

I tweaked it a little to show numbered subqueries:

ygemici.awk:
Code:
{ gsub("\n", " "); $0=$0 }
{ 
if(match($0,"[(].*[)]")) x=$0
subq=1
while(match(x,"[(].*[)]")){
    s=gensub(/.*\(((.[^)]*))\).*/,"\\1",$0);
    if(s~/where/) {
            print "["NR "." subq "] is [[SUCCESS]] ->",s
        } else {
            print "["NR "." subq "] is [FALSE] ->",s
        }
        x=gensub("[(]"s"[)]"," <subquery-" NR "." subq "> ",x)
        subq++
        $0=x
}
if($0~/^$/) next
if($0!~/select|delete|update|insert/)
    print "["NR"]","may not a SQL state ->",$0
else {
    if($0~/where/) print "["NR"]","is [[SUCCESS]] ->",$0
    else print "["NR"]","is [FALSE] ->",$0
    }
}

Code:
# awk -f ygemici.awk RS=';' yourfile
[1.1] is [FALSE] -> select some text
[1.2] is [[SUCCESS]] -> select some text <subquery-1.1>  where some condition
[1] is [[SUCCESS]] -> select some text  <subquery-1.2>  where condition
[2] may not a SQL state ->  some statements some statements some statements
[3] is [[SUCCESS]] ->  update some text where condition
[4] may not a SQL state ->  some statements some statements
[5] is [[SUCCESS]] ->  delete some text where condition
[6] is [FALSE] ->  update some text condition
[7] is [FALSE] ->  delete some text condition

# 17  
Old 01-11-2012
Quote:
Originally Posted by Chubler_XL
Nice solution ygemici,

I tweaked it a little to show numbered subqueries:

ygemici.awk:
Code:
{ gsub("\n", " "); $0=$0 }
{ 
if(match($0,"[(].*[)]")) x=$0
subq=1
while(match(x,"[(].*[)]")){
    s=gensub(/.*\(((.[^)]*))\).*/,"\\1",$0);
    if(s~/where/) {
            print "["NR "." subq "] is [[SUCCESS]] ->",s
        } else {
            print "["NR "." subq "] is [FALSE] ->",s
        }
        x=gensub("[(]"s"[)]"," <subquery-" NR "." subq "> ",x)
        subq++
        $0=x
}
if($0~/^$/) next
if($0!~/select|delete|update|insert/)
    print "["NR"]","may not a SQL state ->",$0
else {
    if($0~/where/) print "["NR"]","is [[SUCCESS]] ->",$0
    else print "["NR"]","is [FALSE] ->",$0
    }
}

Code:
# awk -f ygemici.awk RS=';' yourfile
[1.1] is [FALSE] -> select some text
[1.2] is [[SUCCESS]] -> select some text <subquery-1.1>  where some condition
[1] is [[SUCCESS]] -> select some text  <subquery-1.2>  where condition
[2] may not a SQL state ->  some statements some statements some statements
[3] is [[SUCCESS]] ->  update some text where condition
[4] may not a SQL state ->  some statements some statements
[5] is [[SUCCESS]] ->  delete some text where condition
[6] is [FALSE] ->  update some text condition
[7] is [FALSE] ->  delete some text condition

it was a more great example now Smilie
# 18  
Old 01-11-2012
Hi,
I have the sql file of 250 to 300 lines in that some 10 or 20 statements wil ve select/update or delete. so i don't want to show the rest of the lines which is not select/update/delete on my window.
can you please modify the above code as such pelase

---------- Post updated at 04:51 PM ---------- Previous update was at 04:12 PM ----------

Both the scripts are working like Charm :-)
but I have the sql file of 250 to 300 lines in that some 10 or 20 statements wil ve select/update or delete. so i don't want to show the rest of the lines which is not select/update/delete on my window.
can you please modify the above code as such pelase

---------- Post updated at 05:40 PM ---------- Previous update was at 04:51 PM ----------

one more think is insteard of wring the code in other file and calling it using awk

can we directly put the below line in the code its self and execute it?
Code:
 awk -f ygemici.awk RS=';' yourfile

Moderator's Comments:
Mod Comment Please use next time code tags Last warning!

Last edited by vbe; 01-11-2012 at 09:58 AM..
# 19  
Old 01-11-2012
Quote:
Originally Posted by manasa_vs
Hi,
I have the sql file of 250 to 300 lines in that some 10 or 20 statements wil ve select/update or delete. so i don't want to show the rest of the lines which is not select/update/delete on my window.
can you please modify the above code as such pelase

---------- Post updated at 04:51 PM ---------- Previous update was at 04:12 PM ----------

Both the scripts are working like Charm :-)
but I have the sql file of 250 to 300 lines in that some 10 or 20 statements wil ve select/update or delete. so i don't want to show the rest of the lines which is not select/update/delete on my window.
can you please modify the above code as such pelase

---------- Post updated at 05:40 PM ---------- Previous update was at 04:51 PM ----------

one more think is insteard of wring the code in other file and calling it using awk

can we directly put the below line in the code its self and execute it?
Code:
 awk -f ygemici.awk RS=';' yourfile

Moderator's Comments:
Mod Comment Please use next time code tags Last warning!
save to this a file for example as 'awk_sql.file'
Code:
{gsub("\n","");}
{
if($0~/select|delete|update/)
{
if(match($0,"[(].*[)]"))
{subq=1;
x=$0;while(match(x,"[(].*[)]"))
{
s=gensub(/.*\(((.[^)]*))\).*/,"\\1",$0);suba=" <subquery-" NR "." subq">"suba;
if(subq!=1){sub(">","",suba);suba=suba">"}
if(s~/where/){print "["NR "." subq "] is [[SUCCESS]] ->",s}
else{print "["NR "." subq "] is [[SUCCESS]] ->",s;}
x=gensub("[(]"s"[)]",suba,x);;subq++;$0=x;}
if($0~/where/){print "["NR"]","is [[SUCCESS]] ->",$0;}
else{
print "["NR"]","is [FALSE] ->",$0;}
}
else{
if($0~/where/){print "["NR"]","is [[SUCCESS]] ->",$0;}
else{print "["NR"]","is [FALSE] ->",$0;}
}
}
}

Code:
# awk -f awk_sql.file RS=';' yourinput

regards
ygemici
# 20  
Old 01-11-2012
Quote:
Originally Posted by manasa_vs
one more think is insteard of wring the code in other file and calling it using awk

can we directly put the below line in the code its self and execute it?
Code:
awk -f ygemici.awk RS=';' yourfile

Write this to file process_sql.awk:
Code:
#!/usr/bin/awk -f
BEGIN { RS=";" }
{ gsub("\n", " "); $0=$0 }
{ 
  if(match($0,"[(].*[)]")) x=$0
  subq=1
  while(match(x,"[(].*[)]")){
    s=gensub(/.*\(((.[^)]*))\).*/,"\\1",$0);
    if(s~/where/) {
            print "["NR "." subq "] is [[SUCCESS]] ->",s
        } else {
            print "["NR "." subq "] is [FALSE] ->",s
        }
        x=gensub("[(]"s"[)]"," <subquery-" NR "." subq "> ",x)
        subq++
        $0=x
  }
  if($0~/^$/ || $0!~/select|delete|update|insert/) next
  if($0~/where/) print "["NR"]","is [[SUCCESS]] ->",$0
  else print "["NR"]","is [FALSE] ->",$0
}

Then make sure you set file as executable chmod 755 process_sql.awk

and call it like this:
Code:
$ process_sql.awk yourinput
[1.1] is [FALSE] -> select some text
[1.2] is [[SUCCESS]] -> select some text <subquery-1.1>  where some condition
[1] is [[SUCCESS]] -> select some text  <subquery-1.2>  where condition
[3] is [[SUCCESS]] ->  update some text where condition
[5] is [[SUCCESS]] ->  delete some text where condition
[6] is [FALSE] ->  update some text condition
[7] is [FALSE] ->  delete some text condition


Last edited by Chubler_XL; 01-11-2012 at 05:39 PM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing answers to external program from KSH

I have asked this before but I haven't had any luck so far getting this to work. I use RCS(revision control system). When it runs if I pass the value 'unlock' to $3 its reassigned to $unlock. When I run the command (rcs -u'version number' 'filename') ti will ask me 1-(Do you want to break the lock... (5 Replies)
Discussion started by: pjones006
5 Replies

2. Shell Programming and Scripting

Automating a Java program using KSH

Hi Guys, We have a java program that basically install updates to an existing software. The java program consist of several screens asking user for choices (please see below for a detailed example) *** Screen 1 *** Please choose the operation you want to perform: 1. Install new... (5 Replies)
Discussion started by: maddmaster
5 Replies

3. Shell Programming and Scripting

convert ksh to C program

Hi Guys...is there a way to convert a .ksh script to .C program..? (3 Replies)
Discussion started by: aggars
3 Replies

4. Shell Programming and Scripting

Invoke perl program from Ksh

Hi all, Can I invoke a perl script from Ksh script and pass parameters to the perl script. Please do help me.. thanks Maha (10 Replies)
Discussion started by: mahalakshmi
10 Replies

5. Shell Programming and Scripting

ksh program run with different results by different users

Hi, I wrote a ksh program on Unix. One thing I don't understand: some users run it with different results. I suspect it's either "cat" or "grep" command. Basically, with one group of user, the 'cat' or 'grep' command is not getting the data I need and that changed the result. Is the above... (2 Replies)
Discussion started by: cin2000
2 Replies

6. Shell Programming and Scripting

grep within ksh program

Hi, is there problem with grep command when using ksh? I had the below command: /usr/bin/grep \""$Mon $NewDD\"" /tmp/timemanager/intlog.$$ >> /tmp/timemanager/log.$$ 2>/dev/null when I run ksh in debug mode, this command can not grep anything even the data is in the file. + /usr/bin/grep... (3 Replies)
Discussion started by: cin2000
3 Replies

7. Shell Programming and Scripting

ksh program with password

Hi, I am looking for a way to utilize password when the ksh program is launched. What's the standard or best way to do it? Thanks for your help! (5 Replies)
Discussion started by: cin2000
5 Replies

8. Programming

parameters from my program in c to a .ksh script

hi.. this is my question: it is possible transfer parameters from my program written in C to a .ksh script? how can i do it? i have a program in C, what is called from a .ksh script, and i need what the C program returns some values (parameters) please, help me - any idea thanks ... (2 Replies)
Discussion started by: DebianJ
2 Replies

9. Shell Programming and Scripting

Adding entry into crontab in ksh program

Hi falks, I have the following ksh function ,which adding entry to crontab during ksh program running: { print "Go\c" >/dev/null 2>&1 print '0 0 * * * su - ias -c "/home/orca/core-${SCHEMA_NAME}/CLI/cleanup_BRMS.ksh"\c' >/dev/null 2>&1 print "\033:wq!" >/dev/null 2>&1 } | \crontab -e... (2 Replies)
Discussion started by: nir_s
2 Replies

10. UNIX for Advanced & Expert Users

date program in ksh

#Author : kkodava #!/usr/bin/ksh #Use of this program is You can findout the no of days & day of starting and ending dates #usage no_of_days startdate<yyyymmdd> enddate<yyyymmdd> syy=`echo $1|cut -c1-4` smm=`echo $1|cut -c5-6` sdd=`echo $1|cut -c7-8` eyy=`echo $2|cut -c1-4` emm=`echo... (1 Reply)
Discussion started by: krishna
1 Replies
Login or Register to Ask a Question