how to find matching braces using sed or in shell script | 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.

how to find matching braces using sed or in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-11-2008
Boopesh Boopesh is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 30 September 2009, 3:53 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
how to find matching braces using sed or in shell script

hi,
I want to print all the lines between the matching braces.
For example,the file contains like the below.
asdfsdf
fsdfsd
WO{
w1{
ada
}
w2
{
}
}
sdfsdf
Here i want to print all the lines between WO{ and its corresponding closing brace.
This is very urgent, can somebody help me pls...
Sponsored Links
    #2  
Old 11-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts

Code:
nawk '{
if(index($0,"WO{")!=0)
	f=1
if(index($0,"{")!=0)
	l++
if(index($0,"}")!=0)
	l--
if(f==1 && l>0)
	print
}' filename

Sponsored Links
    #3  
Old 11-12-2008
Boopesh Boopesh is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 30 September 2009, 3:53 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by summer_cherry View Post
Code:
nawk '{
if(index($0,"WO{")!=0)
    f=1
if(index($0,"{")!=0)
    l++
if(index($0,"}")!=0)
    l--
if(f==1 && l>0)
    print
}' filename

Hi..Thanks for the code, but the output is missing the actual brace, instead it is printing the last but one brace.
    #4  
Old 11-12-2008
Boopesh Boopesh is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 30 September 2009, 3:53 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
dude, I got it. I added an extra flag to print that matching brace.
Thank u very much, u made it so simple.!!!
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
Help me to find files in a shell script with any matching pattern hnux Shell Programming and Scripting 3 01-26-2012 10:13 AM
find -regex not recognizing curly braces stevensw Shell Programming and Scripting 4 05-16-2011 12:15 AM
Select block of text around matching braces BootComp Shell Programming and Scripting 2 03-10-2009 03:29 AM
Finding the line number of matching braces Rajendra_1510 Shell Programming and Scripting 3 10-07-2008 05:08 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM



All times are GMT -4. The time now is 02:40 PM.