LinuxQuestions.org
Review your favorite Linux distribution.
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-04-2017, 09:53 AM   #1
csakthikumar
LQ Newbie
 
Registered: May 2017
Posts: 10

Rep: Reputation: Disabled
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.
 
Old 05-04-2017, 10:50 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371
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 View Post
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.
 
Old 05-04-2017, 10:58 AM   #3
csakthikumar
LQ Newbie
 
Registered: May 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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 ""
 
Old 05-04-2017, 11:22 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,235
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
^ is that the forum software eating characters?

i assume you mean
Quote:
find strings that starts with "#define" and ends with "\"
i'd say
Code:
grep -E '^#.*\\$'
 
Old 05-04-2017, 11:33 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,737

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
sed can do more than grep; this one cuts out the macro names
Code:
sed -nr 's/^#[[:space:]]*define[[:space:]]+([^[:space:]]*).*/\1/p' filename
 
Old 05-04-2017, 11:49 AM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,627

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
Perhaps an 'awk' range pattern would select the lines you want.
Code:
awk '/^#define/, !/\\$/ {print}' filename
 
Old 05-04-2017, 11:56 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,737

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
wrong thread, sorry.

Last edited by MadeInGermany; 05-04-2017 at 11:58 AM.
 
Old 05-06-2017, 01:27 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,818

Rep: Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Where To Find The #define minivy Linux - Software 1 12-21-2011 12:42 AM
using find or grep to find a group of text strings. tkmsr Linux - Newbie 3 03-04-2011 07:02 AM
#define and strings rafton Programming 10 01-18-2010 07:41 AM
it starts with choppy DVD video and ends up a crusad IcoNyx Linux - Hardware 4 12-14-2005 04:11 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM

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

All times are GMT -5. The time now is 05:20 AM.

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