The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search for words with capital leters djdaniel3 Shell Programming and Scripting 7 04-06-2008 11:54 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 07:24 AM
Search files that all contain 4 specific words WoodenSword Shell Programming and Scripting 13 01-22-2007 03:57 AM
How to replace a word with a series of words in a file brap45 Shell Programming and Scripting 2 02-20-2006 11:33 PM
search for words in file Agent_Orange Shell Programming and Scripting 4 10-25-2002 02:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-08-2005
Registered User
 

Join Date: May 2005
Posts: 3
Search and replace words between two keywords

Hi,

I have a file which contains the following :

select * from test where test_id=1;

select id
from test1, test2 where test_id=1 and test_id=2;

select * from
test1, test2, test3 where test_id=4 and test2_id where in (select test2_id from test2);

select
id1, id2 from test
where test_id=1;

............................

I need all words between first "from" (including) and first "where" condition (excluding) to be deleted irrespective of whether they span on multiple lines ...

ie., the required output is :

select * where test_id=1;
select id where test_id=1 and test_id=2;
select * where test_id=4 and test2_id=6 where in (select test2_id from test2);
select id1, id2 where test_id=1;


Thanx

-RR
Reply With Quote
Forum Sponsor
  #2  
Old 05-09-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
No doubt there is a better way but this sure seemed to work for me:
Code:
nawk '
    BEGIN{RS=";"}
    {
        gsub(/[Ff][Rr][Oo][Mm].*[Ww][Hh][Ee][Rr][Ee]/,"where",$0)
        gsub(/\012/,"",$0)
        printf("%s;\n",$0)
    }' yourfilename
My only issue is that there is an extract semicolon at the end of2the output.

Code:
select * where test_id=1;
select id where test_id=1 and test_id=2;
select * where in (select test2_id from test2);
select id1, id2 where test_id=1;
;
Thomas
Reply With Quote
  #3  
Old 05-09-2005
Registered User
 

Join Date: May 2005
Posts: 3
Thanx a lot ... yeah it does work ... but on some statements like these :

select * from test WHERE test_id in (select test_id1 from test1 where test_id1=1);

the output is :

select * where test_id1=1);

I need it as :

select * WHERE test_id in (select test_id1 from test1 where test_id1=1);

ie., it needs to cut at the first where condition ... is there a way to specify this ?

Thanx

-RR
Reply With Quote
  #4  
Old 05-10-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
Sorry, my goof. Change the first gsub to sub.

Code:
nawk '
    BEGIN{RS=";"}
    {
        sub(/[Ff][Rr][Oo][Mm].*[Ww][Hh][Ee][Rr][Ee]/,"where",$0)
        gsub(/\012/,"",$0)
        printf("%s;\n",$0)
    }' yourfilename
New output:
Code:
select * where test_id=1;
select id where test_id=1 and test_id=2;
select * where test_id=4 and test2_id=6 where in (select test2_id from test2);
select id1, id2 where test_id=1;
;
Reply With Quote
  #5  
Old 05-10-2005
Registered User
 

Join Date: May 2005
Posts: 3
Hmm ... I still don't seem to be getting the required output ...

nawk 'BEGIN{RS=";"}{sub(/[Ff][Rr][Oo][Mm].*[Ww][Hh][Ee][Rr][Ee]/,"where",$0);gsub(/\012/,"",$0);printf("%s;\n",$0)}' test.sql

Output :

select * where test_id=1);

cat test.sql

select * from test WHERE test_id in (select * from test1 where test_id=1);

Required output :

select * where test_id in (select * from test1 where test_id=1);

Words between first "from" and first "where" condtion (case insensitive, exact match)
need to be removed ...

Thanx

-RJ
Reply With Quote
  #6  
Old 05-10-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Problem is that the regular expression /[Ff][Rr][Oo][Mm].*[Ww][Hh][Ee][Rr][Ee]/ will match the largest matching pattern, i.e. from the first "from" to the last "where".

You could try using /[Ff][Rr][Oo][Mm][^\(]*[Ww][Hh][Ee][Rr][Ee]/ which works on the example given but may not work in all cases.
Reply With Quote
  #7  
Old 05-11-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
Quote:
Originally Posted by Ygor
Problem is that the regular expression /[Ff][Rr][Oo][Mm].*[Ww][Hh][Ee][Rr][Ee]/ will match the largest matching pattern, i.e. from the first "from" to the last "where".

You could try using /[Ff][Rr][Oo][Mm][^\(]*[Ww][Hh][Ee][Rr][Ee]/ which works on the example given but may not work in all cases.
Good catch; I hadn't got back to see were my error was. I also agree that there are likely to be cases that tis script doesn't with.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0