Search and replace words between two keywords


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace words between two keywords
# 1  
Old 05-08-2005
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
# 2  
Old 05-09-2005
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
# 3  
Old 05-09-2005
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  
Old 05-10-2005
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;
;

# 5  
Old 05-10-2005
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  
Old 05-11-2005
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  
Old 05-11-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

4. Shell Programming and Scripting

Extracting words and lines based on keywords

Hello! I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here: 1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script? For example: Given a keyword "world" in the line: ... (2 Replies)
Discussion started by: seemad
2 Replies

5. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

search and replace combination of two words...with a constraint

Hi I have 100 files in my directory. Please help me how to do in Unix or any other scriptin lanuages. I want to replace all occurances of "goutham" to goutham_ind ONLY if the file contains the word "goutham" with the word "engineer"; for eg----test1 is a file contains the following inf; goutham... (6 Replies)
Discussion started by: nandugo1
6 Replies

8. Shell Programming and Scripting

Search a file with keywords

Hi All I have a file of format asdf asf first sec endi asdk rt 123 ferf dfg ijglkp (7 Replies)
Discussion started by: mailabdulbari
7 Replies

9. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies
Login or Register to Ask a Question