grep source code and exclude comments


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users grep source code and exclude comments
# 1  
Old 01-15-2010
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
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
Code:
 
$ 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:
Code:
 
void    type_func(int rate)
        type = BIN_RATE + vrate;        // current Bin type
        if(!type)

# 2  
Old 01-15-2010
First eliminate comment lines and then you can remove them:

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

I used "#" to represent a beginning of comment out line
# 3  
Old 01-15-2010
Hi.

Perhaps this might help: https://www.unix.com/unix-dummies-que...mand-help.html, changing this part:
Code:
# replace // comment with nothing
/^[ \t]*\/\//d


Last edited by Scott; 01-17-2010 at 08:28 AM..
# 4  
Old 01-15-2010
Not fully tested

Quote:
sed "/^ *\/\*/,/*\/ *$/d" file
# 5  
Old 01-16-2010
What about letting the preprocessor to do the work? Using gcc:
Quote:
cpp -fpreprocessed foo.c | grep type
HTH,
Loïc.
# 6  
Old 01-16-2010
hello ,

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

Regards,
# 7  
Old 01-16-2010
Quote:
Originally Posted by gaurav1086
hello ,

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

Regards,
Personally, I think the preprocessor option fits the bill perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude multiple lines using grep

Hi, I'm working on a shell script that reports service status on a database server. There are some services that are in disabled status that the script should ignore and only check the services that are in Enabled status. I output the service configuration to a file and use that information to... (5 Replies)
Discussion started by: senthil3d
5 Replies

2. Shell Programming and Scripting

Grep last two lines, calc & adding comments

....... 06/09/2013|12:00:00 PM|3|26112|40|44032|27419.7|6 1 0 93 |6|1|0|93 06/09/2013|12:30:00 PM|3|26112|40|44032|27491|11 4 0 85 |11|4|0|85 I have "sysperf.out" file containing the lines above. What I like to have on the output is: Node: prod1db ===> this is the hostname Date:... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

3. Shell Programming and Scripting

Exclude dash in grep

Hi, I must be overlooking something, but I don't understand why this doesn't work. I'm trying to grep on a date, excluding all the lines starting with a dash: testfile: #2013-12-31 2013-12-31code: grep '^2013-12-31' testfileI'm expecting to see just the second line '2013-12-31' but I don't... (3 Replies)
Discussion started by: Subbeh
3 Replies

4. Shell Programming and Scripting

Grep for a srting & exclude two folders

Hi, Below is the command to grep for a string under grep -r "redeem" /home/tom Need to make it case insensitive and exclude logs & tmp folders under /home/tom directory in my Search. Need this in Linux. (1 Reply)
Discussion started by: mohtashims
1 Replies

5. Shell Programming and Scripting

Using Grep Include/Exclude Files

I wrote this korn script and ran into a hole. I can use find to exclude all the hidden directories and to use my include file/exclude files for running a full backup find / -depth -ipath '/home/testuser/.*' -prune -o -print| grep -f include.mydirs | grep -v -f exclude.mydirs but when I... (8 Replies)
Discussion started by: metallica1973
8 Replies

6. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

7. UNIX for Dummies Questions & Answers

grep exclude/find single and double quotes

Hello, I'm trying to use grep or egrep to exclude a whole range of characters but how do I exclude both a single and a double quote. It might be easier to say how do I use grep to find both single and double quotes. grep ' ' " ' file grep detects the first single quote within my... (4 Replies)
Discussion started by: Lindy_so
4 Replies

8. UNIX for Advanced & Expert Users

how to exclude the GREP command from GREP

I am doing "ps -f" to see my process. but I get lines that one of it represents the ps command itself. I want to grep it out using -v flag, but than I get another process that belongs to the GREP itself : I would like to exclude # ps -f UID PID PPID C STIME TTY TIME CMD... (2 Replies)
Discussion started by: yamsin789
2 Replies

9. Shell Programming and Scripting

grep - to exclude lines beginning with pattern

11132 13069 11137 11142 13070 Can I use grep command to exclude all lines beginning with 13? I dont want to use grep -v 13 as potentially there will be a number with something like 11013 that I would exclude in error.. (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question