Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

range patterns in awk

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-20-2010
ghoda2_10 ghoda2_10 is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 26 December 2010, 1:42 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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:


Code:
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:


Code:
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??

Last edited by radoulov; 12-20-2010 at 08:31 AM.. Reason: Code tags, please!
Sponsored Links
    #2  
Old Unix and Linux 12-20-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts
It would be easier if you post a representative sample of your data (i.e. similar to your real file) and the expected output.
Sponsored Links
    #3  
Old Unix and Linux 12-20-2010
ghoda2_10 ghoda2_10 is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 26 December 2010, 1:42 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Below are two functions in the file

Code:
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:

Code:
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 "}":

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


Last edited by Scott; 12-20-2010 at 09:06 AM.. Reason: Code tags, please...
    #4  
Old Unix and Linux 12-20-2010
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 22 September 2016, 3:57 AM EDT
Location: IN
Posts: 894
Thanks: 15
Thanked 183 Times in 174 Posts
try as..

Code:
awk '/.*function.*menu/,/^}/' inputfile > outfile
or
sed '/menu/,/^}/!d' inputfile > outfile

Sponsored Links
    #5  
Old Unix and Linux 12-20-2010
anurag.singh anurag.singh is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 12 July 2012, 8:07 AM EDT
Location: Hyderabad, India
Posts: 413
Thanks: 13
Thanked 99 Times in 96 Posts

Code:
 
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


Last edited by anurag.singh; 12-20-2010 at 10:45 AM..
Sponsored Links
    #6  
Old Unix and Linux 12-20-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 8 December 2016, 11:07 AM EST
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Quote:
Originally Posted by ghoda2_10 View Post
Below are two functions in the file

Code:
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:

Code:
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 "}":

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

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

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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Searching patterns in 1 file and deleting all lines with those patterns in 2nd file toms Shell Programming and Scripting 4 08-14-2009 07:53 AM
print range between two patterns if it contains a pattern within the range joyan321 Shell Programming and Scripting 2 06-18-2009 06:27 PM
How to get value between patterns sirababu Shell Programming and Scripting 6 03-25-2009 05:05 AM
get the value between 2 patterns minifish Shell Programming and Scripting 11 04-07-2008 03:18 PM
search patterns rochitsharma Shell Programming and Scripting 1 02-27-2006 04:14 AM



All times are GMT -4. The time now is 07:48 AM.