Operating System - Linux
1752584 Members
4447 Online
108788 Solutions
New Discussion юеВ

Re: How i can delete one line in all my files?

 
SOLVED
Go to solution
Francesco_13
Regular Advisor

How i can delete one line in all my files?

Hi,

i'm do it to delete all the characters that are delimited by: /* any character */, in all my text files .How i can do it?
I'm in HP-UX .

Thanks.
Best regards.
5 REPLIES 5
Simon Hargrave
Honored Contributor
Solution

Re: How i can delete one line in all my files?

To do one file (and to test the pattern), open it up in vi and run the substitution: -

%s/\/\*.*\*\///g

When you're happy it works, use "ed" to run this against all your files: -

for FILE in *.txt
do
ed $FILE <1,\$s/\/\*.*\*\///g
w
q
EOF
done

(note the escaped $, this is so the shell doesn't interpret it)

This will remove everything between /* and */ on a line.

Note that this won't remove multi-line comments, but then your subject implies one line so I assumed this to be your requirement.
Peter Godron
Honored Contributor

Re: How i can delete one line in all my files?

Francesco,
please have a look at:
http://sed.sourceforge.net/grabbag/scripts/

Section: Beautifiers

H.Merijn Brand (procura
Honored Contributor

Re: How i can delete one line in all my files?

When you want to do that reliable all by your self, you are bound to get stuck somewhere. Some possible problems

1. Comments that cross lines:

/* Comment
* blah
*
*/

2. Comments that look like to be terminated

/* Don't do \*/ that here */

3. Other strangenesses

a = b*/*/*/*3/*/*/*3;

4. Unicode

/*├п┬╝ *├в *├в *├п┬╝ */

(contains 0uff0a 0u2215 and 0u2044)

If you use perl and install Regexp::Common, you can use Regexp::Common::comment, which should be safe for all of the above
http://search.cpan.org/~abigail/Regexp-Common-2.120/
http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common/comment.pm


use Regexp::Common qw(comment);

while (<>) {
/$RE{comment}{C}/ and print "Contains a C comment\n";
}

use Regexp::Common qw(comment RE_comment_C);

while (<>) {
$_ =~ RE_comment_C () and print "Contains an C comment\n";
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: How i can delete one line in all my files?

Hi,

as procura brought Perl into play,
the removal of C comments seems such a common task that there already must exist dozens of solutions.
Probably overkill, but I remembered having come accross a parsing grammar for C comments in the POD of Damian Conway's Parse::RecDescent.

http://search.cpan.org/~tbone/Parse-RecDescent-FAQ-3.94/FAQ.pm#Removing_C_comments
Madness, thy name is system administration
Francesco_13
Regular Advisor

Re: How i can delete one line in all my files?

Thanks to all !