grep source code and exclude comments

I often find myself grepping source code for a variable name and many times the name would be present in comment lines that I 'd prefer not to see. Do you guys know any tricks to filter out comments?

Example:

snippet of the source code

 
/***
 * type comment 1
 ***/
void    type_func(int rate)
{
  int   vrate = rate < 0 ? rate / (-2) : rate * (+2);
        type = BIN_RATE + vrate;        // current Bin type
        bshx = MAX_TYPE + vrate;
        // no type available
        if(!type)
                return;

plain grep results

 
$ grep type c.c
 * type comment 1
void    type_func(int rate)
        type = BIN_RATE + vrate;        // current Bin type
        // no type available
        if(!type)

I'd like to be able to see only this:

 
void    type_func(int rate)
        type = BIN_RATE + vrate;        // current Bin type
        if(!type)

First eliminate comment lines and then you can remove them:

sed -e 's/#.*//;/^$/d'  FILE

I used "#" to represent a beginning of comment out line

Hi.

Perhaps this might help: http://www.unix.com/unix-dummies-questions-answers/15578-sed-script-command-help.html, changing this part:

# replace // comment with nothing
/^[ \t]*\/\//d

Not fully tested

What about letting the preprocessor to do the work? Using gcc:

HTH,
Lo�c.

hello ,

If I were you , I'd better try cscope
Vim/Cscope tutorial

Regards,

Personally, I think the preprocessor option fits the bill perfectly.

The preprocessor option is great idea, thanks a lot.