![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
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; ; |
|
#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 |
|
#4
|
|||
|
|||
|
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
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; ; |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
Quote:
|
|||
| Google The UNIX and Linux Forums |