having trouble getting 'sed' to delete comment lines in source files

having trouble getting 'sed' to delete comment lines in source files

Post by Peter Eak » Thu, 28 Jun 2001 05:54:03



I would like to use a 'sed' script to temporarily delete comment lines
(among other things) in java source files.  I thought this would be
easy using regular expressions to specify the matching lines and sed's
'd' (delete) command.  But it doesn't seem to work for single line
comments, such as:
        /* single line comment */

it's as if it doesn't see the close-comment string ("*/") if it's on
the same line as the start-comment string ("/*").  I've tried this on
Solaris and NT with MKS with similar results.  I've read the docs and
don't see what I'm doing wrong.  Does anyone know 'sed' and can see
what I'm doing wrong?

Here's a sed command to delete comments:

        sed -e '/^[     ]*\/\*/,/\*\//d' test

here's the 'test' file referenced in the above command:

        start
         /* single line comment */
        middle
         /* multiline
        comment */
        after

the "middle" line gets eaten somehow by sed, and the only output I see
is:

        start
        after

It's as if sed is not looking for the close comment string on the same
line as the begin comment string and deletes everything down to the
second "*/".

Any suggestions appreciated.

Thanks.

        -Pete

(my oppologies and please let me know if I'm posting this to the wrong
group)

 
 
 

having trouble getting 'sed' to delete comment lines in source files

Post by Michael Heimin » Thu, 28 Jun 2001 07:01:13



> I would like to use a 'sed' script to temporarily delete comment lines
> (among other things) in java source files.  I thought this would be
> easy using regular expressions to specify the matching lines and sed's
> 'd' (delete) command.  But it doesn't seem to work for single line
> comments, such as:
>         /* single line comment */

> it's as if it doesn't see the close-comment string ("*/") if it's on
> the same line as the start-comment string ("/*").  I've tried this on
> Solaris and NT with MKS with similar results.  I've read the docs and
> don't see what I'm doing wrong.  Does anyone know 'sed' and can see
> what I'm doing wrong?

> Here's a sed command to delete comments:

>         sed -e '/^[     ]*\/\*/,/\*\//d' test

> here's the 'test' file referenced in the above command:

>         start
>          /* single line comment */
>         middle
>          /* multiline
>         comment */
>         after

> the "middle" line gets eaten somehow by sed, and the only output I see
> is:

>         start
>         after

> It's as if sed is not looking for the close comment string on the same
> line as the begin comment string and deletes everything down to the
> second "*/".

> Any suggestions appreciated.

> Thanks.

>         -Pete

> (my oppologies and please let me know if I'm posting this to the wrong
> group)

Using your input, 'grep -v "/*/" infile' works,
'awk '!/\/\*/ && !/\*\// {print $0}' infile' could work better.

Michael Heiming

 
 
 

having trouble getting 'sed' to delete comment lines in source files

Post by Dave Granti » Thu, 28 Jun 2001 11:31:43


Pete, here's your problem:

Quote:>    sed -e '/^[     ]*\/\*/,/\*\//d' test

Translating your sed into english, this script says,
Delete all lines between the (1) first line beginning with 0 or more
spaces (or tabs!) and (2) the next line containing a */...

sed isn't seeing the end of the single line for the second half of
your delete.

Try something like:
sed  -e '/^[       ]*\/\*.*\*\//d' -e '/^[      ]*\/\*/,/\*\//d' test

Note the first sed block looks for 0 or more whitespace chars,
followed by /*, followed by (greedy) zero or more any chars, followed
by */.  All on one line.  Then it gets deleted.  The second piece
never sees the "/* --- */" on a single line.

Cheers



> I would like to use a 'sed' script to temporarily delete comment lines
> (among other things) in java source files.  I thought this would be
> easy using regular expressions to specify the matching lines and sed's
> 'd' (delete) command.  But it doesn't seem to work for single line
> comments, such as:
>    /* single line comment */

> it's as if it doesn't see the close-comment string ("*/") if it's on
> the same line as the start-comment string ("/*").  I've tried this on
> Solaris and NT with MKS with similar results.  I've read the docs and
> don't see what I'm doing wrong.  Does anyone know 'sed' and can see
> what I'm doing wrong?

> Here's a sed command to delete comments:

>    sed -e '/^[     ]*\/\*/,/\*\//d' test

> here's the 'test' file referenced in the above command:

>    start
>     /* single line comment */
>    middle
>     /* multiline
>    comment */
>    after

> the "middle" line gets eaten somehow by sed, and the only output I see
> is:

>    start
>    after

> It's as if sed is not looking for the close comment string on the same
> line as the begin comment string and deletes everything down to the
> second "*/".

> Any suggestions appreciated.

> Thanks.

>    -Pete

> (my oppologies and please let me know if I'm posting this to the wrong
> group)

Dave

+---------------------+---------------+


+---------------------+---------------+

 
 
 

having trouble getting 'sed' to delete comment lines in source files

Post by Peter S Tillie » Wed, 25 Jul 2001 16:59:42



Quote:> Pete, here's your problem:

> > sed -e '/^[ ]*\/\*/,/\*\//d' test

> Translating your sed into English, this script says,
> Delete all lines between the (1) first line beginning with 0 or more
> spaces (or tabs!) and (2) the next line containing a */...

> sed isn't seeing the end of the single line for the second half of
> your delete.

> Try something like:
> sed  -e '/^[       ]*\/\*.*\*\//d' -e '/^[ ]*\/\*/,/\*\//d' test

> Note the first sed block looks for 0 or more whitespace chars,
> followed by /*, followed by (greedy) zero or more any chars, followed
> by */.  All on one line.  Then it gets deleted.  The second piece
> never sees the "/* --- */" on a single line.

> Cheers

<snip>

There are, of course even more problems if you have comments on the same
line as pieces of code.  For example,

    ctr = ctr + 1; /* asinine comment about incrementing ctr */

or, even worse,

    /* pointless initial comment */ ctr = ctr + 1; /* even more pointless
    comment that doesn't even finish on the same line */

and I'm sure we've all seen some like these.

Neither of these is exactly trivial to deal with in sed or awk.

Peter
--


Opinions expressed are my own and not necessarily those of my employer

 
 
 

1. "rm: can't unlink 'files'",can't delete files

Hi,
I copied files from ntfs partition with "cp -r" to
a ext3 linux partion.
Every source file's hard link number is 1.  However,
some destination files's hard link number changed.
Is it nornmal? What made this change?

When I tried to delete these destination files,
shell showed me "rm:can't unlink XXX :Permission denied"
(XXX are file's name).  This happened to files no matter
what their hard link number is, 1 or more.

I ran as the owner of these files, why still permission
denied?  Is there any way I can know where the other
hard link, pointing to the same disk space, is?

Anyone can tell me what caused this and how to solve
it?

Thanks
--
We never meet....

2. true color in windows, 16color in linux

3. Getting unresolved symbols for 'new' and 'delete' operators

4. how to add more etherport(>8)to linux?

5. Slow 3d Performance

6. 9FX REALITY 332

7. sed: file sedd line 13: Unknown command: ``-''

8. Using sed or awk to remove #'s from commented lines.

9. how to append one file's line to another file's line?

10. 'delete' command in ftp delete files in local hard disk ???

11. I'm having trouble setting up a SLIP line