Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w # (invoke as e or ee) # Please see the POD for install and licensing details use strict; ###### globals my $version = "0.9"; my $comm; my @stripped; my $topline; ###### how we were called chomp(my $us = qx!basename $0!); if ( $us eq "ee" ) { $comm = ';'; } else { $comm = '#'; } ###### parse args $#ARGV >= 2 && die("\n No more than 2 arguments\n\n"); defined $ARGV[0] || die(&usage($us)); my $ifile=$ARGV[0]; -e $ifile || die("\n Input file nonexistent.\n\n"); open(IFIL,"<$ifile") or die("problem opening input_file"); my @inputfile=<IFIL>; close(IFIL); ###### main if ( $us eq "ee" ) { $topline = shift(@inputfile); die(&pwarn($comm)) if $topline =~ /\#\!.*perl/i ; unshift(@inputfile,$topline); &stripper(@inputfile); } elsif ( $us eq "e" ) { $topline = shift(@inputfile); if ( $topline =~ /(\s+)\#\!/ ) { &stripper(@inputfile); unshift(@stripped,$topline); } else { unshift(@inputfile,$topline); &stripper(@inputfile); } } ###### final output if ( $ARGV[1] ) { open(OFIL,">$ARGV[1]") or die("problem creating output_file"); for ( @stripped ) { print OFIL "$_\n"; } print "\n Done stripping $ifile\n -\> wrote output file \"$AR +GV[1]\"\n\n"; close(OFIL); } else { for ( @stripped ) { print "$_\n"; } } exit $?; ###### subs sub stripper { for ( @_ ) { chomp; next if /^$comm|^(\s*)$comm|^(\s*)$/; $_ =~ s/$comm.*$//; push(@stripped,$_); } return @stripped; } sub usage { print qq[ Usage: e filename [outputfilename] ииииииииииииииииииииииииииииииииииииииииииииииииииииииииии +ииииии e strips comments and blank lines from an existing file. e to remove # comments, and ee to strip ; comments. See "perldoc" ииииииииииииииииииииииииииииииииииииииииииииииииииииииииии +ииииии v$version invo +ked as \'$us\' ]; exit(1); } sub pwarn { print qq[ WARNING: Input file "$ifile" looks like a Perl script The first line was: $topline When invoked as \'$us\', strips out semicolons, which might not be very useful for looking at a Perl scrip +t. If this assumption is wrong, remove the first line tempora +rily. ]; &usage; exit(1); } __END__ =head1 NAME e (and ee), symbolic links to =head1 VERSION Version 0.9 =head1 SYNOPSIS e (, to be invoked as either "e" or "ee") e args ee args =head1 DESCRIPTION B<e> (invoked as "e" or "ee") is a small program to strip unix style c +omments ( e.g., "#" or ";" ) from scripts and configuration files. It + might be useful during system administration. It is called "e" simply for brev +ity. B<e> also removes blank lines, makes some effort not to destroy shell +scripts and shebangs, and tries to avoid mangling Perl scripts it enc +ounters. B<e> is meant to be run on Unix systems where #, #!, and ; are common +comments/patterns. B<e> requires at least one argument, a filename to be processed. B<e> tries to detect if the first line of the input file contains the +#! character sequence, and tries to preserve it, assuming it might be + a shell script. B<e> will stop and warn you about removing semi-colons from a file it +thinks is a Perl script. =head1 INSTALLATION Install the main file,, somewhere in your path, then in the same +directory, do ln -s e ln -s ee Use e or ee, depending on what character you want to strip. Invoking directly breaks it. If you already have an e or ee on your system, you may use other symbo +lic links, If you rename these files, you will have to adjust the main script acc +ordingly. =head1 EXAMPLES =over 4 =item B<e> I<input_filename> Strips # comments and blank lines out of "filename" and sends the resu +lt to your screen. =item B<e> I<input_filename> [I<output_filename>] Same as above, but the result will be written to a new file "output_fi +lename" in the current directory. =item B<ee> I<input_filename> [I<output_filename>] Same as above, but semicolon as the comment character. =back =head1 BUGS Might not be able to preserve the shebang line in a shell script, when + the shebang line is preceded by one or more blank lines. =head1 LIMITATIONS Does not remove C style comments. Inefficiently written, so uses lots of memory when input files get lar +ger. Cannot detect a "here" document, and will happily destroy the contents + of one when it encounters a comment character somewhere in there. =head1 AUTHOR Harold Sinclair devel at hastek =head1 COPYRIGHT Copyright Е2004 hastek. All rights reserved. This program is free software; you can redistribute it and/or modify i +t under the same terms as Perl itself. =cut #EOF

In reply to Comment Stripper script for unix by hsinclai

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (2)
    As of 2018-03-18 02:40 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (228 votes). Check out past polls.