Grep the whole body of a function

awkgrepsedvim

Supposing I know that someFile.php contains the definition for someFunction(). How would I go about displaying the whole body of the function in stdout? If I know that the body is 10 lines long then I would use cat someFile.php | grep -A 10 "function someFunction" [1] but in reality the function could be any arbitrary length.

I figured that with sed I could use Vimesque commands such as /function someFunction<Return>/{<Return>% [2] but I can't figure out exactly how to format it. Ideally, the PHPDoc before the function would be output along with the function.

Any help or links to the appropriate fine manual would be appreciated. Thanks!

[1] I know that the cat is redundant, but I find this format easier to read.

[2] Find the function definition, go to the opening brace, go to the close brace

Best Answer

Identify a regular expression that occurs at the end of the function but not in its body.  ^} will probably work.

sed –n '/function someFunction/,/^}/p' someFile.php

Obviously, if the PHPDoc before the function begins with some unique string, you would use that rather than /function someFunction/.  Otherwise, it’s not so easy; it might be possible with awk.

Related Question