4

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

5

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.

  • 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

Your Answer

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

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