LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-26-2007, 06:41 PM   #1
HiOctane21
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: OpenBSD, Ubuntu, RedHat, Fedora Core, OpenSuse
Posts: 57

Rep: Reputation: 15
Remove Java code comments


Hi,

Can someone suggest a shell script solution to remove Java comments.

I'm able to remove the //.... comments from lines that have no space or text before:

Code:
sed '/^\//d' file.java
But I'm trying to also remove the java comments that are on the same line as the java code and the ones after some tabs or blank spaces.
Also I'd like to remove the comments that are within /*.....*/

Any help is appreciated.
 
Old 04-26-2007, 07:22 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Here's one for the /* ... */. You should be able to modify it to adjust for //.

Code:
[root@log01 tmp]# cat javacomments
/* this is crap */
/*I hate comments*/

real code goes here
[root@prdlog01 tmp]# cat javacomments | sed 's/\/\*.*\*\///g'



real code goes here
[root@log01 tmp]#
-twantrd
 
Old 04-26-2007, 07:27 PM   #3
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
i didn't write the code for you, but ideal (pseudocode )probably like this:

declare linenumberstartcomment=0, linumberstopcomment=0, l=0,begincomment=0;

for i in originalfilecontent.java
if found "/*" in `echo $i`
{ linenumberstartcomment=l
begincomment=1;
}

elseif begincomment = 0 || found "*/" in `echo $i`
begincomment=0;

if begincomment=0
echo $i>> newjava.java

l=l+1
next

...


something like this.
 
Old 04-26-2007, 07:34 PM   #4
HiOctane21
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: OpenBSD, Ubuntu, RedHat, Fedora Core, OpenSuse
Posts: 57

Original Poster
Rep: Reputation: 15
Thanks...

I tried twantrd's solution and it works for /*...*/ comments.
I'll try turning kstan pseudocode into a shell script and will let you know.
 
Old 04-26-2007, 09:34 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
import re
pat = re.compile('\/\*(.*?)\*\/',re.M|re.DOTALL)
data = open("sourcefile").read()
print pat.sub("",data)
sample input:
Code:
first line /* this is crap */
/*hat comments */ 2nd line /*I hate comments*/
/* comments */
code goes here
output:
Code:
# ./test.py
first line
 2nd line

code goes here
 
Old 04-26-2007, 10:06 PM   #6
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Quote:
I tried twantrd's solution and it works for /*...*/ comments.
I'll try turning kstan pseudocode into a shell script and will let you know.
What's wrong with modifying mine? Just curious...
-twantrd
 
Old 04-27-2007, 01:43 AM   #7
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
I think the comment probably in this pattern

/*Developer: alibaba
*this is the comment
*return int test
*/
public ......


thats why i give another pseudocode.
 
Old 04-28-2007, 12:45 AM   #8
HiOctane21
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: OpenBSD, Ubuntu, RedHat, Fedora Core, OpenSuse
Posts: 57

Original Poster
Rep: Reputation: 15
Kstan...
How would you interpret "if found" in a csh script?

Thanks
 
Old 04-28-2007, 09:39 AM   #9
HiOctane21
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: OpenBSD, Ubuntu, RedHat, Fedora Core, OpenSuse
Posts: 57

Original Poster
Rep: Reputation: 15
People!!!...Look what I found

http://sed.sourceforge.net/grabbag/s.../remccoms3.sed
 
Old 04-28-2007, 09:52 AM   #10
DamianS
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Rep: Reputation: 0
Nice find. Should work for php and javascript code as well
 
  


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
Using egrep to display C++ code without comments ckoniecny Linux - General 2 11-07-2006 11:49 PM
Removing comments from C++ source code Nylex Programming 8 11-22-2005 04:41 AM
LD linker doesn't remove unreachable code ? vips Programming 3 09-08-2004 08:12 AM
Remove program which installed by source code cccc Linux - Software 3 09-09-2003 01:24 PM
remove source code? secprovider Linux - General 2 08-16-2003 08:18 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration