开发者

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

开发者 https://www.devze.com 2022-12-27 22:56 出处:网络
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

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:

/////////////////////////////////////////
// FIX开发者_运维问答MESOON, 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 context. 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!


You can use pcregrep with the -M option (multiline matching; pcregrep is grep with Perl-compatible regular expressions). Something like:

pcregrep -M ";*\R*.*thingtosearchfor*\R*.*;.*"


Here's an example using awk.

$ cat file
blah1
blah2
  function1 ("test",
                    MY_CONSTANT,
                    (some *really) - long / expression);

function2( one , two )
blah3
blah4

$ awk -vRS=")" '/function1/{gsub(".*function1","function1");print $0RT}' file
function1 ("test",
                    MY_CONSTANT,
                    (some *really)

the concept behind: RS is record separator. by setting it to ")", then every record in your file is separated by ")" instead of newline. This make it easy to find your "function1" since you can then "grep" for it. If you don't use awk, the same concept can be applied using "splitting" on ")".


You can write a command line using grep with the options that give you the line number and the filename, then xarg these results into awk to parse these columns and then use a little script from you to display the N lines surrounding that line? :)


If this isn't an academic endeavour you could just use cscope (for C code only though). If you are willing to drop the requirement to search in comments ctags should be enough (and it also supports Perl).


I had a situation in which I had an xml file full of the names of zip files in an xml style format, that is, with carrots bracketing the names of the files, say example.zip<\stuff>

I used awk to change all carrots into newlines then used grep :)

0

精彩评论

暂无评论...
验证码 换一张
取 消