Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need only the first occurances of these statements from the group

Status
Not open for further replies.

IMAUser

Technical User
May 28, 2003
121
CH
Hi ,
I have a SQL file with a set of CREATE OR REPALCE statements. There are always two sets for every object created. I need the first occurance of each of these statements...

Like for example...

#my_orig_file.sql
CREATE OR REPLACE view test1
as select sysdate from dual;
CREATE OR REPLACE view test2
as select sysdate from dual;

CREATE OR REPLACE view test11
as select sysdate from dual;
CREATE OR REPLACE view test12
as select sysdate from dual;

What I need is the resultant file should have
CREATE OR REPLACE view test1
CREATE OR REPLACE view test11

What I tried doing was as below

#get_the_lines.awk
{
if ( var==0 && /CREATE OR REPLACE VIEW/ ) { print ; var=1 }
else { var=0 }
}

and call it
PROMPT> nawk -f get_the_lines.awk my_orig_file.sql

The thinking is that, set a variable and match the string. So print the first occurance and ignore the next.

The script above is giving me all the occurances and not just 1st, 3rd, 5th etc...

Any ideas please.

Thanks.
 
Thanks for the response feherke.
But if I dont reset the var after every print then I will get back all the occurances. Whereas I am only after 1st, 3rd, etc. So I am saying
Is the var !set and is the statement CREATE OR REPLACE
if yes print the line and set the var.

So for the next record, since the var is set , there will be no printing and only seting of var.

Is there a flaw in here....

Thanks.

 
Hi

This is what I suggested and for me outputs what you specified :
Code:
{
if ( var==0 && /CREATE OR REPLACE VIEW/ ) { print ; var=1 }
[red]if (!NF)[/red] { var=0 }
}


Feherke.
 

Probably I am not doing something right, but it dosent seem to work for me....
 
Hi

Or you gave us wrong data. For example you have lowercase "view" then matching uppercase "VIEW". That should not work. If still works for you, you posted wrong data.

Feherke.
 
nawk '/CREATE OR REPLACE/{++var;if(var%2==1)print}' my_orig_file.sql

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Hey PHV. That works. Thanks.

Sorry feherke. It works on the small set of test script posted above, but not on the original huge SQL file.

I had constructed the test file by copying the text I need to look for(CREATE OR REPLACE VIEW), from the huge SQL file. So it is not the case mismatch. It is still giving me both the occurances and not just the 1st, 3rd, etc.

But thanks for your help.





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top