LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find strings that starts with #define and ends with \ (https://www.linuxquestions.org/questions/linux-newbie-8/find-strings-that-starts-with-define-and-ends-with-%5C-4175605245/)

csakthikumar 05-04-2017 09:53 AM

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.

rtmistler 05-04-2017 10:50 AM

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 (Post 5706142)
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.

csakthikumar 05-04-2017 10:58 AM

Quote:

Originally Posted by rtmistler (Post 5706174)
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 ""

ondoho 05-04-2017 11:22 AM

^ 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 '^#.*\\$'

MadeInGermany 05-04-2017 11:33 AM

sed can do more than grep; this one cuts out the macro names
Code:

sed -nr 's/^#[[:space:]]*define[[:space:]]+([^[:space:]]*).*/\1/p' filename

allend 05-04-2017 11:49 AM

Perhaps an 'awk' range pattern would select the lines you want.
Code:

awk '/^#define/, !/\\$/ {print}' filename

MadeInGermany 05-04-2017 11:56 AM

wrong thread, sorry.

grail 05-06-2017 01:27 AM

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


All times are GMT -5. The time now is 02:35 AM.