How to apply brackets?

Hi,

I have a query term like this:

$query="apple NOT banana AND fruits";

$query="a1 NOT prim1 AND a2 NOT a3 OR a5";

I want to apply brackets to the NOT terms and the output should be like this:

$query="apple NOT (banana) AND fruits";
$query="a1 NOT (prim1) AND a2 NOT (a3) OR a5";

How can i do this??

Regards
Vanitha

Try this:

awk '{for(i=1;i<=NF;i++){if($i=="NOT"){i++;$i="("$i")"}}}1'

Regards

Hi,

How can i do that in perl???

Regards
Vanitha