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 06-22-2009, 04:00 PM   #16
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347

Looking at the awk program suggested by gostdog74, it struck me that it might be interesting to modify that program to handle the case of nested comments. So, for what it's worth, here's my take on the code:
$ cat igor.awk             
/\(*/ || /\*\)/ {
  while (match($0,/\(\*/)) {
    if (!f) printf "%s", substr($0, 1, RSTART-1)
    $0 = substr($0, RSTART+2)
  while (match($0,/\*\)/)) {
   (f) ? --f : 0
   if (!f && (RSTART+2 < length($0))) {
    print substr($0,RSTART+2)
   $0 = substr($0, RSTART+2)
   if (!f && !NF) printf "\n"
!f && NF {print}
Here's my test data
$ cat test.dat
This is a word (* followed by comment *)
This is a line with a (* split
comment over three
lines *)
And one with no comments
So what's this (* gonna do
if there's *) anything behind comments?
And (* sometimes (* we

*) might have
other *)
nested comments.
and the output:
$ awk -f igor.awk test.dat
This is a word
This is a line with a
And one with no comments
So what's this  anything behind comments?
nested comments.
Old 06-22-2009, 08:38 PM   #17
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
good attempt, but i sure hope OP knows how to comment his code properly.


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
How to remove a value with sed mierdatuti Programming 3 12-18-2008 05:36 PM
RHEL 5 Kickstart copying files from DVD & inserting comments with sed ilo Linux - Enterprise 0 01-31-2008 12:01 PM
using sed to remove all but ip addresses chess Programming 10 07-02-2007 01:54 AM
Remove Java code comments HiOctane21 Programming 9 04-28-2007 09:52 AM
How sed strip /* comments aaronzh Programming 1 06-05-2003 05:16 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:35 PM.

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