Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to search a substantial C++ codebase for a single line that is outputing text to the terminal that I'd like to suppress. I have grepped for std::cout and have had around 40 different files returned. The problems I face are:

  • I did not add this myself so I have no idea where it is.
  • It is a pointer only outputing memory location so I have no context in which to search for it.
  • The codebase is enourmous and contains a great many other instances of sdt::cout that have once been used for debugging purposes and have since been commented out.

My question pertains to the last one. I am using

grep -rle 'std::cout' .

to search, which will return positive for instances of std::cout, //std::cout, // std::cout and any other occurence of std::cout sitting on a line that is actually commented out.

How can I modify my grep to omit any line containing // so I can eliminate the commented lines?

share|improve this question
1  
This might solve your problem, without -l: grep -re 'std::cout' | grep -v '//'. –  Tim N Sep 30 '11 at 14:37
    
From your description, it looks like you wouldn't want to match this line: std::cout << p; // just debug data –  Tim N Sep 30 '11 at 14:38
    
"The codebase is enourmous and contains a great many other instances of sdt::cout that have once been used for debugging purposes and have since been commented out." This is why you create a separate function for logging debugs and use a define to enable/disable it. –  Ignacio Vazquez-Abrams Sep 30 '11 at 15:16

3 Answers 3

egrep -r '^([^/]/?)*std::cout' .
share|improve this answer

You could look for std::cout only when it isn't preceded by //. Regexp syntax does not include negation; every regexp can be negated by writing out its complement, but the complement can grow huge. Here, it's not too big:

grep -rl -E '^/?([^/]/?)*std::cout' .

You can also list all occurrences of std::cout and filter away the occurrences of //.*std::cout, but note that this will hide things like std::cout << foo; // std::cout << bar;.

grep -r 'std::cout' . | grep -vE '^[^:]*:.*//.*std::cout' | sed -e 's/:.*//'

Alternatively, you can run a tool that parses the C++ code, such as ctags.

share|improve this answer
grep -r 'std::cout' . | grep -v '^[ \t]*//'

will only omit the commented out lines, and not all the lines that have a comment on them.

share|improve this answer
2  
-l suppresses normal output, so you'd just filter filenames containing comments. –  Tim N Sep 30 '11 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.