The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 09:29 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM
Need help matching a number x96riley3 Shell Programming and Scripting 1 04-04-2006 12:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-05-2008
Rajendra_1510 Rajendra_1510 is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces.

The file contents are as follows

File XXX.dat

1 ( CLASS "FRUIT"
2 (TYPE "PERSISTENT")
3 (MESSAGE_TYPE "M")
4 (GET_REQRD "Y")
5 (SET_REQRD "Y")
6 )
7 ( CLASS "VEGETABLES"
8 (TYPE "PERSISTENT")
9 (MESSAGE_TYPE "N")
10 (GET_REQRD "N")
11 )
12 ( CLASS "SEED"
13 (TYPE "PERSISTENT")
14 (MESSAGE_TYPE "N")
15 (SET_REQRD "N")
16 )


Now I want to get the line numbers 7and 11 that matches braces of class
"VAGETABLES". 7 is the line number of starting brace of class "VEGETABLES" and 11 is the line number of ending brace of CLASS "VEGETABLES".I want the starting and ending line number to be stored in two different variables,say START_LINE_NUMBER and END_LINE_NUMBER.
Can you please help me?
  #2 (permalink)  
Old 10-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,372

Code:
eval "$(
   awk '/"VEGETABLES"/ {
     printf "start=%d\n", NR; n = 1d", NR; n = 1
   }
/^)$/ && n == 1 {
     printf "end=%d\n", NR; exit
 }' )"

  #3 (permalink)  
Old 10-06-2008
Rajendra_1510 Rajendra_1510 is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
Thank u for ur replu..Its working
  #4 (permalink)  
Old 10-07-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
try below perl script




Code:
$class=shift;
open (FH,"<filename") or die "Can not open file!";
while(<FH>){
	if(m/$class/){
		print "Starting ",$.,"\n";
		$flag=1;
	}
	if(m/^\)/ && $flag==1){
		print "Ending ",$.,"\n";
		$flag=0;
	}
}
close(FH);

Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0