LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2010, 09:56 AM   #1
mapasserby
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
Grep - searching for class references in C++ using regular expression


I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp.
I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working as I espected:

grep -E '^[^(\/\*)(\/\/)].*[^a-zA-Z]ABCZ[]*[\*\(\<:;,{& ]'

^[^(\/\*)(\/\/)] don't math comments in the begging of the line ( // or /* )
.* followed by any character
[^a-zA-Z] do not accept any caracter before the one I'm searching (like defABCZ)
[]* any white space (I can have something like ABCZ var; )
[\*\(\<:;,{& ] followed by ( * < : ; , & { (I cant get #define "ABCZ.h" or ABCZdef for example)

Well, I can get patterns like this:

class Test: public ABCZ{
class Test: public ABCZ {
class Test : public ABCZ<T>
class NameSpace::Test : public ABCZ<T>
ABCZ var = ABCZ();
ABCZ* var = new ABCZ();
ABCZ* var = new ABCZ<T>();
teste = ABCZ(var);
teste = ABCZ(var, otherVar);
teste = ABCZ(var,otherVar);
teste = ABCZ();
funct(*ABCZ av);
struct(ABCZ, BBBB, CCCC);
const ABCZ& getNot_before(void) const;

That's all right, they are all acceptable patterns. But these I'm missing, and they are all acceptable too:

ABCZ teste;
ABCZ teste; //comment
ABCZ teste; /* comment*/
ABCZ teste; /* comment*/
ABCZ teste;
ABCZ *teste;

And all these are not acceptable:
#include "ABCZ.h"
//ABCZ var = ABCZ();
ABCZdef a = ABCZdef();
DefABCZ a = DefABCZ();
ABCZdef a = ABCZdef();
/*ABCZ var = ABCZ();

I don't know if I'm missing any other patterns, if I am, please tell me =p
What am I doing wrong?
I'll be very grateful if someone answer me
Thanks in advance

Last edited by mapasserby; 05-28-2010 at 09:57 AM.
 
Old 05-28-2010, 10:50 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Strong suggestion:
1. Forget about an ad hoc regular expression
<= It's a good thing to know...
... but it's probably not the best tool for this job

2. Instead, use a tool like id-utils:
http://www.gnu.org/software/idutils/manual/idutils.html
http://www.drdobbs.com/184401358;jse...OSKHWATMY32JVN

a) Install GNU id-utils (it might have different names, depending on your distro and package manager. Or you can just download it directly from GNU)

b) Run "mkid" on your source tree

c) Do a "gid" of your header

d) Voila! Problem solved!

IMHO .. PSM
 
Old 05-31-2010, 06:45 AM   #3
mapasserby
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you, paulsm4, for your answer!
You had a good idea, I didnt know idutils. I tried it here, and I'm not sure if it is what I need. I forgot to say that what I really want to do it's to search for references in a specific file, to know if its includes are usuless or not. So I use doxygen to generate all files dependences and I get all includes of any files and I see if this include is being used or not.
I dont know if I'm right, but it looks like me that idutils can only search in all files specified with the mkid command, and I already know exactly each file I have to seek. So, I think it doesnt help me, because it would work just like a grep =/ or am I wrong? That's the first time I try idutils.
Any other good idea?
Thanks a lot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to use regular expression in grep? sagarkha Linux - Newbie 6 01-23-2010 08:15 AM
grep regular expression casperdaghost Linux - Newbie 2 08-15-2009 11:01 AM
Searching for somewhat regular expression sebelk Linux - General 5 07-30-2009 01:45 PM
Searching for regular expression in command line Jean Of mArc Programming 9 08-11-2008 08:01 PM
Help with grep regular expression lamar_air Programming 3 07-21-2006 11:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration