range patterns in awk

Hi All,

I am new to awk command and I had a question in using it.

I want to filter a code file and print specific functions (that contain menu word in the function name).

for example, if the file has:

function menu1()
{
}
 
function f2()
{
}
 
function menu3()
{
}

so I want the command to print the first and third functions only.
I wrote the below command using range patterns:

awk '
/.*function.*menu\(\)/,/^ *\} *$/
'

but there is a problem, the above one prints the function until the first "}" even if it is not the ending of the function , is there a way to fix this??

It would be easier if you post a representative sample of your data (i.e. similar to your real file) and the expected output.

Below are two functions in the file

function $reset_report(), invisible
{
 a = set_value;
 return @true;
}

function $reset_palette_menu(),INDIRECT
{
  if ( v == "")
    $notify(message1, @warning);
  else {
    $replace_palette();
   }
 b = value; 
}

and I need the output to be:

function $reset_palette_menu(),INDIRECT
{
  if ( v == "")
    $notify(message1, @warning);
  else {
    $replace_palette();
   }
 b = value; 
}

but the command I wrote above only prints the function until the first "}":

function $reset_palette_menu(),INDIRECT
{
  if ( v == "")
    $notify(message1, @warning);
  else {
    $replace_palette();
   }

try as..

awk '/.*function.*menu/,/^}/' inputfile > outfile
or
sed '/menu/,/^}/!d' inputfile > outfile
 
awk '/.*function.*menu.*/ {f=1;print;}
/\{/ {if(f==1) a++;}
/\}/ {if(f==1) {a-- if(a==0) c="}"}}
{if(f==1 && a>0) print; if(f==1 && c=="}") {print c; c=""; f=0;}}' inputFile

This also works with more curly brackets {} in your function:

awk '/reset_report/{f=1;getline} f{c+=/}/?1:/{/?-1:0}; !f; f && !c{f=0}' file