Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
Thanks, Scott, that is great! Tell me, if I end the line with * instead of someFile.php, is there any way to have sed print out the file names before the function? – dotancohen Nov 27 '12 at 23:05
@dotancohen: I don’t see how. That would definitely be easy in awk, though. – Scott Nov 27 '12 at 23:11
I see, thanks Scott. – dotancohen Nov 27 '12 at 23:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.