How do I grep for entire, possibly wrapped, lines of code?

Posted by NXT on Stack Overflow See other posts from Stack Overflow or by NXT
Published on 2010-04-22T04:47:34Z Indexed on 2010/04/22 4:53 UTC
Read the original article Hit count: 206

Filed under:
|
|
|
|

When searching code for strings, I constantly run into the problem that I get meaningless, context-less results. For example, if a function call is split across 3 lines, and I search for the name of a parameter, I get the parameter on a line by itself and not the name of the function.

For example, in a file containing

...
  someFunctionCall ("test",
                    MY_CONSTANT,
                    (some *really) - long / expression);

grepping for MY_CONSTANT would return a line that looked like this:

                    MY_CONSTANT,

Likewise, in a comment block:

/////////////////////////////////////////
// FIXMESOON, do..while is the wrong choice here, because
// it makes the wrong thing happen
/////////////////////////////////////////

Grepping for FIXMESOON gives the very frustrating answer:

// FIXMESOON, do..while is the wrong choice here, because

When there are thousands of hits, single line results are a little meaningless. What I would like to do is have grep be aware of the start and stop points of source code lines, something as simple as having it consider ";" as the line separator would be a good start.

Bonus points if you can make it return the entire comment block if the hit is inside a comment.

I know you can't do this with grep alone. I also am aware of the option to have grep return a certain number of lines of content. Any suggestions on how to accomplish under Linux? FYI my preferred languages are C and Perl.

I'm sure I could write something, but I know that somebody must have already done this.

Thanks!

© Stack Overflow or respective owner

Related posts about unix

Related posts about linux