LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   regular expression to match comments in C (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expression-to-match-comments-in-c-890857/)

alexandnpu 07-09-2011 09:49 PM

regular expression to match comments in C
 
my task is to retrieve a certain pattern from c source code , but the i don't need the comments, so i have to remove them.

----------------the first question-----------------
i have done some processing , the code and the matched pattern are in the same line, i need to match the comments and remove them.
the example goes as follows:

/*******abc*****/ int j = 0; /***abc/cde****/ int i = 3;

all my work is done is in bash script, so your help in bash script is much appreciated.

i have already got this one : awk '{gsub("/\\*[^/]*\\*/","");print}'

but it cannot process the second comment in the example.
so could you give me some help? can I use / and * altogether as one search symbol?



----------------the second question-----------
i need to use commas to separate the code line. but some lines confuses me :
foo ("abc, dec" "we are here", 1, "true");
i need to retrieve the the string "abc, dec we are here", the comma cause me headache.
could you give me some help?



thanks a lot.

grail 07-10-2011 01:22 AM

Your first problem has 2 issues:

1. awk can use either // to search for a regex or "" to compute a regex. You have used both

2. You are not looking for the unique item to exclude or better way to say it is remove so many lots of not this.
Code:

awk 'gsub(/\/[^ ]*/,"")'
With question 2 i would need to know which version awk you are using? Version 4 (gawk) just released a few days ago will make this simple, but previous versions will require
some finagling.


All times are GMT -5. The time now is 11:36 AM.