range patterns in awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

range patterns in awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 07:31 AM.. Reason: Code tags, please!
Sponsored Links
    #2  
Old 12-20-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 26 September 2014, 7:14 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,674
Thanks: 182
Thanked 620 Times in 578 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 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 08:06 AM.. Reason: Code tags, please...
    #4  
Old 12-20-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
try as..

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

Sponsored Links
    #5  
Old 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 98 Times in 95 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 09:45 AM..
Sponsored Links
    #6  
Old 12-20-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 29 September 2014, 12:58 PM EDT
Location: The Netherlands
Posts: 7,717
Thanks: 123
Thanked 537 Times in 507 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:53 AM
print range between two patterns if it contains a pattern within the range joyan321 Shell Programming and Scripting 2 06-18-2009 05:27 PM
How to get value between patterns sirababu Shell Programming and Scripting 6 03-25-2009 04:05 AM
get the value between 2 patterns minifish Shell Programming and Scripting 11 04-07-2008 02:18 PM
search patterns rochitsharma Shell Programming and Scripting 1 02-27-2006 03:14 AM



All times are GMT -4. The time now is 08:14 AM.