I'm working with a preprocessed C source file. I want to extract one function for additional processing. I can find the start of the particular function with:

gcc -DNDEBUG -march=native -E source.c > source.pre

BEGIN=$(grep -n 'static inline int some_function' source.pre | cut -f 1 -d ':')

After the above executes, I have a line number where the function begins. In this case, its 7812.

I am having trouble devising a way to capture the text between the C function's curly braces ({ and }). Based on Extract middle section of lines of a text file, I think this is reducing to a problem of finding the immediate closing brace (}).

How do find the immediate closing brace? Or how do I capture the text between curly braces in C preprocessed file?


Assuming nothing wacky in the source that might place a stray } on a line by itself, try sed.

sed -n '/^static inline int some_function(/,/^}$/p' source.pre
  • Oh, thanks. That's much easier than the path I was going down. – user56041 Apr 18 '16 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy