Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-09-2011, 09:49 PM   #1
LQ Newbie
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
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.
Old 07-10-2011, 01:22 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,618

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


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
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 02:59 PM
Regular expression to match 4 or more alpha characters sixerjman Programming 15 11-27-2006 12:03 AM
Don't match a regular expression dakensta Programming 7 09-21-2006 03:48 AM
perl regular expression a char match richikiki Programming 8 07-19-2006 03:37 AM
A regular expression to avoid comments enemorales Programming 2 02-07-2006 08:22 AM > Forums > Linux Forums > Linux - Newbie

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

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