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

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

Thank u for ur replu..Its working:)

try below perl script

$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);