LinuxQuestions.org
Visit Jeremy's Blog.
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 06-22-2009, 05:00 PM   #16
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346

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:
Code:
$ cat igor.awk             
#!/bin/gawk
/\(*/ || /\*\)/ {
  while (match($0,/\(\*/)) {
    if (!f) printf "%s", substr($0, 1, RSTART-1)
    ++f
    $0 = substr($0, RSTART+2)
  }
  while (match($0,/\*\)/)) {
   (f) ? --f : 0
   if (!f && (RSTART+2 < length($0))) {
    print substr($0,RSTART+2)
    next
   }
   $0 = substr($0, RSTART+2)
   if (!f && !NF) printf "\n"
  }
}
!f && NF {print}
Here's my test data
Code:
$ 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:
Code:
$ 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?
And
nested comments.
 
Old 06-22-2009, 09:38 PM   #17
ghostdog74
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.
 
  


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


All times are GMT -5. The time now is 08:43 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration