Help in retrieving the ending line numbers of the functions

Hi! I've a C file which consist of many function definitions with numbers at the beginning as shown below..


10 void search()
11 {
12 /*body
14 *
15 *
17 *
18 *
40 *
42 *
60 }
90 void func_name()
95 {
99 /*body
100 *
105 *
111 *
112 *
130 *
135 }


I need to retrive the number at which the function begins and ends..
I mean the function 'search' begins at number 10 and ends at number 60.
In the same way the function 'fun_name()' begins at number 90 and ends at 135..
I can somehow get the beginning line number of the functions using grep..
But how can I get the ending line number??
Note: The numbers used will not be in the order, and there might be many opening and closing braces in the functions..

How can I do it??
Thanks in advance.. :slight_smile:

Can you post/attach a sample input file for testing. And you will have to do like parsing way than just using grep, using grep may not cover exactly what we are looking for.

Try:

awk '/\(\)/&&i==0;/{/{i++}/}/&&i--&&i==0' file

Sample file for what??

I hope the sample you have posted here doesn't cover all the cases, so we need big file to test our logic.

Hi sorry couldn't reply.. Was on an educational trip.. Ya check the attachment for the sample program..

Did you try my code posted above? I'll repost it...

awk '/\(\)/&&i==0;/{/{i++}/}/&&i--&&i==0' file
1 Like

Hi!! Got it!! Thanks!! :slight_smile: