views:

78

answers:

3

Are there any tools available that will search through a file, treating it as code, rather than plain text? That is, so you could specify that you want to find all occurrences of "foo" contained in a string? ..or in a variable name, etc?

A quick web search turned up Codase but it seems to be more of a search engine rather than something you could use at a command line.

Ideally, I'm looking for something I could integrate into my IDE via some command line scripting/macros.

+1  A: 

If you're a fan of UNIXy things, some combination of exuberant ctags and ack can do a pretty good job. They integrate really well with vim (which is my IDE of choice).

Ben Hughes
+1 for ctags and ack
docgnome
A: 

Eclipse has that kind of searching support out of the box – at least for Java.

In general you need at least some kind of crude parserfor the langauge you're interested in so the search can know which kind of token is it when one is found.

Joey
+1  A: 

The SD Source Code Search Engine is probably what you are looking for. It uses language-aware scanners (C, C++, C#, Java, PHP, COBOL, JCL, PLSQL, ECMAScript, ...) to convert each source file into a set of index terms for that source file, with index terms taken to be langauge elements such as specific keywords ('for', 'begin', ....), string literals (of whatever several varieties might exist in the langauge), variable names, operators, comments. Whitespace is ignored as appropriate for each language. It handles ASCII, ISO8859-1 and Unicode based langauges. Using index terms for each file it builds a global index for a large source code base (tens of millions of lines).

A GUI offers you a query langauge for searches. You can find just strings containing abc:

 S=*abc*

or constants larger than 20:

 N>20

or for loops with a comparison to an upper bound involving an a variable postfixed with the name "Income"

'for' ... '<=' I=*Income

Search are often shorter than a second using the Linux kernal (1.5 MSLOC, 9500 files).

Hits on search terms pull up a list of lines containing the hits. Clicking a line pulls up the source text, properly tab-indented, with accurate line numbers (those of you that use GCC will appreciate this since GCC counts form characters oddly). Selecting EDIT takes you to your favorite editor.

EDIT 2/22/2010: It has recently acquired a command-line invocation method, so you can use it rather like grep.

Ira Baxter