print only comments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print only comments
# 1  
Old 06-12-2010
print only comments

I want to write a shell script which it takes as argument a java file or a c++ file (.java or .cpp).
It will check if the file is type of java or c++, else it ends with error message.
If all are ok, it will call awk that prints only the comments that the java or c++ file contains, grouping and puting them between tags.

For example, I have this code:
Code:
/*
The HelloWorld class implements an application that
simply prints "Hello World!" to standard output.
//You can add comments
*/
class HelloWorld {
    public static void main(String[] args) {
//Let's print Hello World
        System.out.println("Hello World!");
    } /* some comments */
} //end of class

I want to have this output:
Code:
<comments>
/*
The HelloWorld class implements an application that
simply prints "Hello World!" to standard output.
//You can add comments
*/
/* some comments */
//end of class
</comments>

# 2  
Old 06-12-2010
Quote:
Originally Posted by Mark_orig
Code:
//Let's print Hello World

I guess this one is also a comment ^^

Check this:
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $mlcom=0; # multiline comment
my $line;
my @outarr;

if (@ARGV == 0) { print "Missing argument!\n"; exit 1; }

if (@ARGV >= 2) { print "Too many arguments!\n"; exit 1; }

my $infile="$ARGV[0]";
my $outfile='comments.dat';

open(I,$infile) or die "Error opening input file: $!\n";
my @data=<I>;
close(I);

push @outarr, "<comments>\n";

 foreach my $line (@data) {
 chomp($line);

 if ($mlcom == 1 && $line !~ /\*\//) {
  push @outarr, "$line\n";
  next;
 }
 elsif ($mlcom == 1 && $line =~ /\*\//) {
 push @outarr, "$line\n";
 $mlcom=0;
 next;
 }

 if ($line =~ /\/\*/ && $line =~ /\*\//) {
 $line =~ s!^[^/]+!! ;
 push @outarr, "$line\n";
 next;
 }
 elsif ($line =~ /\/\//) {
 $line =~ s!^[^/]+!! ;
 push @outarr, "$line\n";
 next;
 }
 elsif ($line =~ /^\/\*/ && $line !~ /\*\//) {
 $mlcom=1;
 push @outarr, "$line\n";
 next;
 }
 else {
 next;
 }
}

push @outarr, "</comments>\n";

open(O,">$outfile") or die "Error opening output file: $!\n";
print O @outarr;
close(O)


Script demonstration / Test run:
Code:
$ cat myfile.cpp
/*
The HelloWorld class implements an application that
simply prints "Hello World!" to standard output.
//You can add comments
*/
class HelloWorld {
    public static void main(String[] args) {
//Let's print Hello World
        System.out.println("Hello World!");
    } /* some comments */
} //end of class
$ 
$ ./pickcomm.pl
Missing argument!
$ 
$ ./pickcomm.pl myfile.cpp another.cpp
Too many arguments!
$ 
$ ./pickcomm.pl myfile.cpp 
$
$ cat comments.dat
<comments>
/*
The HelloWorld class implements an application that
simply prints "Hello World!" to standard output.
//You can add comments
*/
//Let's print Hello World
/* some comments */
//end of class
</comments>
$



---------- Post updated at 02:21 ---------- Previous update was at 02:17 ----------

Let me know if this Perl script is an option for you, if yes, I could add file type checking of the argument (file name).
Aiiyeh, I see you want them grouped too ... (that should also be no problem: simply creating 2 (or 3) arrays for 2 (or 3) types of comments)

Last edited by pseudocoder; 06-12-2010 at 09:28 PM..
# 3  
Old 06-13-2010
Thank you for the answer, but I want something defferent (no Perl) , like this:
Code:
#!/bin/sh

if [ -f  $1 ]; then
echo " file $1 exists"
else
echo "file $1 does not exists"
fi
exit 1

#  but I want to check if file is .java or .c++ not a simple file (-f), I don't know how to do it..
# if all are ok I call awk

BEGIN awk'

regular expression for comments /*...*/, //.

END{print comments}'

# 4  
Old 06-13-2010
Code:
#!/bin/sh

if [ -f  $1 ]; then
echo "file $1 exists"
else
echo "file $1 does not exists"
exit 1 #needs to be before the "fi" line, else the script will never run the remaining parts of the script.
fi

ext=$(echo $1 | sed 's/.*\.//')

if [ "$ext" = "c++" ] || [ "$ext" = "java" ]; then
echo "file $1 is a .$ext file"
else
echo "file $1 is neither a .c++ nor a .java file!"
exit 1
fi

# 5  
Old 06-13-2010
When I run it , it prints this, not the comments:

./code.txt HelloWorld.java
Code:
file HelloWorld.java exists
file HelloWorld.java is a .java file

# 6  
Old 06-13-2010
Yeah, because I (apart from the Perl script) only helped you to solve this problem:
# but I want to check if file is .java or .c++ not a simple file (-f), I don't know how to do it..

The part with awk and regex (the hardest part IMHO) is still open.
This User Gave Thanks to pseudocoder For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Delete Comments

Hello i am back :D, i have a prolem. I want to Delete the IPs which are in Comments. Input 192.168.0.1 192.168.0.2 #192.168.0.3 #192.168.0.4 - when TAB or Space, delete too. /*192.168.0.5 192.168.0.6 192.168.0.7*\ Output 192.168.0.1 192.168.0.2 My solution is sed -e... (7 Replies)
Discussion started by: eightball
7 Replies

2. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

3. Shell Programming and Scripting

delete comments

Delete everything comes in between /* & */. Current File: ==================== create or replace procedure test421 is begin /* ---sasasas/*dsdsds */ dbms_output.put_line('SAURABH'); END; To be File: =================== create or replace procedure test421 is begin... (10 Replies)
Discussion started by: susau_79
10 Replies

4. Shell Programming and Scripting

Scripting comments

Anyone have a good link for documentation conventions for scripts? (4 Replies)
Discussion started by: gliesian
4 Replies

5. Programming

lint comments

Hi can anyone help me regarding the meaning of the following lint messages. what is the use of having such lint comments in the c program. /*lint -esym(534,cputs,fgets,cprintf) */ /*lint -efile(766,pragmas.h) */ Thanks a lot in advance. (5 Replies)
Discussion started by: axes
5 Replies

6. Shell Programming and Scripting

Comments unexpectedly print

Hi All, I am working on a sh script. Half way through the script, my comments start to print to screen as if I used echo. Obviously any comments are preceded with a #. And I have not used set -x or anything similar. Can anyone free me of this simple but annoying problem? Thanks John (5 Replies)
Discussion started by: John H
5 Replies

7. UNIX for Dummies Questions & Answers

Print comments in FTP

Dear Unix Gurus, Not sure if this is possible/workable but I'm trying add a comment line before each operation such as the following: ftp -n <mail server> ${log_dir}/test_put.log << END user <user_id> <password> verbose bin # List Files Before putting Data echo File Check Before FTP >>... (7 Replies)
Discussion started by: lweegp
7 Replies

8. UNIX for Dummies Questions & Answers

Remove comments...

It may be a no-brainer, but the answer is escaping me right now: I'm trying to write a little script to remove all comments from .c source... I was thinking sed, but I'm not a very strong regexp user (e.g. I suck with sed). I tried dumping the file into: sed -e 's/\/\* * \*\///g' and several... (1 Reply)
Discussion started by: LivinFree
1 Replies
Login or Register to Ask a Question