search for a pattern using awk between two words

Hi,
how can we search for a pattren between two words? below are the examples

input
1)select from table_name c1,c2,c3,c4,fn(),fn2(),c5;-->false
2)select from table_name c1,c2,c3,c4;--True
3)select from table
c1,
c2,
c3,
fn(),
c4;-->true
4)select from table_name
c1,
c2,
c3;-->true

in the above 4 examples
1) the pattren will be same each sellect will end with ; and start with "select from" in between it may have column names and function calls.
we have to search from "select from" till ; and check if functions are called inside .
2) if something with ( and ) the its function call and we should print false for that. else true.

could some one help he getting this?

 
$ nawk 'BEGIN{RS="\;"}{if($0~/\(/){print $0 "-->false"}else{print $0 "-->true"}}' input.txt

Similar to the above solution, but with the semi colons printed and only for lines with select. One quirk is the spurious semicolon on the last line.

awk '{sub(/$/,RS)} /select/{s=/\(\)/?"true":"false";sub(/$/,"-->"s)}1' RS=\; infile

Thanks both the code is working fine..

but for the statements like below though function call is not there it is showing as false the below cases also shoulf be true.

select some text
(select some text(select some text)
where some condition)
where condition

this should be true. since there are no function calles

---------- Post updated at 12:09 PM ---------- Previous update was at 12:05 PM ----------

in this case the above code should print false where as it is printing true.

select some text
(select some text(select some text)
where some condition)
where condition;--true

Since my statement tests for "()" it should signal false. Do these select statements also get terminated with ;?
EDIT: I tried it and it returns false. Are you sure that is the result you are getting?

yes it wil end with ;
sorry for confusion,for your case it should say true.

Found it. I had true and false reversed:

awk '{sub(/$/,RS)} /select/{s=/\(\)/?"false":"true";sub(/$/,"-->"s)}1' RS=\; infile

it still has problem
select some text
(select some text(select some text)
where some condition)
where condition;-->false;
select cl,c2,c3,c4,fn(),c5-->false;
select c1,c2,c3-->true;

for the first select it should be true.
for 2nd and 3rd statement its correct.

With this input file:

select some text
(select some text(select some text)
where some condition)
where condition;
select cl,c2,c3,c4,fn(),c5;
select c1,c2,c3;

and using this:

awk '{sub(/$/,RS)} /select/{s=/\(\)/?"false":"true";sub(/$/,"-->"s)}1' RS=\; infile

I get:

select some text
(select some text(select some text)
where some condition)
where condition;-->true
select cl,c2,c3,c4,fn(),c5;-->false
select c1,c2,c3;-->true
;

Is this not what you are getting?
Are you using Solaris?

1 Like

Can we print only the false statements than printing all the other statements and true statements?

Do you mean like this?

awk '/select/ && /\(\)/'  RS=\; ORS=\; infile

Yes :smiley:
Thanks.