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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-04-2017, 10:53 AM
|
#1
|
LQ Newbie
Registered: May 2017
Posts: 10
Rep:
|
Find strings that starts with #define and ends with \
hi All,
I am trying to perform optimization of the macros definition in my C code. For the same I need to extract all the macros present in the C files and I need to find how many times this specific macro definition has been present.
The format of my macro definitions are in such a way that
#define MACRO_NAME DEFN_LINE1\
DEFN_LINE2\
DEFN_LINE3
So I thought my logic will be to
1) use regular expression to find the list of lines that starts with "#define" and ends with "" redirect the above output to a file say MacroLineExtract.txt
2) remove all "#define " from MacroLineExtract.txt ==> MACRO_NAME DEFN_LINE1\
3) remove the trailing strings after the space ===> MACRO_NAME
4) save the above output to file Macros.txt which will now just hold the list of MACROS present in my code
5) Write a bash script to take one line after another from Macros.txt and find how many times that MACRO_NAME has appeared in my code.
Can you please help me in writing a regex to find all the macros in my c files which starts with "#define" and ends with "". If you have better logic please suggest the same.
|
|
|
05-04-2017, 11:50 AM
|
#2
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,938
|
Hi csakthikumar and welcome to LQ.
Kind of difficult to understand what you're talking about until you write the line near the end:
Quote:
Originally Posted by csakthikumar
Can you please help me in writing a regex to find all the macros in my c files which starts with "#define" and ends with "".
|
To me this would be the FIND command. SED also uses REGEX to locate and replace, AWK is also pretty good with this.
I feel you need to be more clear about your intentions, as well as clarify what ends with "" truly means, because "" could represent nothing or could represent two quote symbols in series.
|
|
|
05-04-2017, 11:58 AM
|
#3
|
LQ Newbie
Registered: May 2017
Posts: 10
Original Poster
Rep:
|
Quote:
Originally Posted by rtmistler
Hi csakthikumar and welcome to LQ.
Kind of difficult to understand what you're talking about until you write the line near the end:To me this would be the FIND command. SED also uses REGEX to locate and replace, AWK is also pretty good with this.
I feel you need to be more clear about your intentions, as well as clarify what ends with "" truly means, because "" could represent nothing or could represent two quote symbols in series.
|
Hi rtmistler,
I want to find strings that starts with "#define" and ends with "" - grep "^['#';]*define" vpi_def.h can find all the lines starting with "#define" can you please help me in modifying the grep to find line ending with ""
|
|
|
05-04-2017, 12:22 PM
|
#4
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
^ is that the forum software eating characters?
i assume you mean
Quote:
find strings that starts with "#define" and ends with "\"
|
i'd say
|
|
|
05-04-2017, 12:33 PM
|
#5
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,012
|
sed can do more than grep; this one cuts out the macro names
Code:
sed -nr 's/^#[[:space:]]*define[[:space:]]+([^[:space:]]*).*/\1/p' filename
|
|
|
05-04-2017, 12:49 PM
|
#6
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,493
|
Perhaps an 'awk' range pattern would select the lines you want.
Code:
awk '/^#define/, !/\\$/ {print}' filename
|
|
|
05-04-2017, 12:56 PM
|
#7
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,012
|
wrong thread, sorry.
Last edited by MadeInGermany; 05-04-2017 at 12:58 PM.
|
|
|
05-06-2017, 02:27 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
I would have said awk and just do the job once:
Code:
awk '/#define.*\/$/{cnt[$2]++}END{for(i in cnt)print i,"appears ",cnt[i],"times"}' c_file_here
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 11:14 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|