it is awful to read huge config files full with remarks. remrem.pl removes all remarks and blank lines to concentrate config files to the essence. it has two output possiblities and even more using the shell. (updated 20010914, using HERE documents, the rest I left in peace for your convenience to see the differences to Hofmators way.)
#!/usr/local/bin/perl -w sub usage { print <<EOF; 'REMoveREMarks'! remrem.pl deletes blank lines or lines starting with a '#' from a giv +en file-one and prints it to STDOUT or writes it to file-two Syntax : $0 inputfile # STOUT, try $0 inputfile | less $0 inputfile outputfile #Output tho <outputfile> EOF } if ( (! $ARGV[0] ) or ( $ARGV[0] eq "--help") ) { usage; exit; } open(FUN,"<$ARGV[0]")||die "Can't open file $ARGV[0] for reading"; if ($ARGV[1]) { open(GUN,">>$ARGV[1]")||die "Can't open file $ARGV[1] for writing" } foreach $zeile (<FUN>) { $zaehler++; if ( ( ! ($zeile=~/^(#+)/ ) ) and ( $zeile=~/\w/) ) { if ($ARGV[1]) { print GUN $zeile } else { print "$zaehler: $zeile" } } } if ($ARGV[1]) { close(GUN)||die "Can't close filehandle $ARGV[1]." } close(FUN)||die "Can't close filehandle $ARGV[0]."

Replies are listed 'Best First'.
Re: remrem.pl - remove remarks
by Hofmator (Curate) on Sep 03, 2001 at 16:51 UTC

    Some suggestions for improvement:

    • Use HERE-docs
    • Use the available special variables ($. == line number)
    • Include the error variable $! in your die statements
    • Use a while loop instead of a foreach loop as the latter reads the whole file into memory. This is totally unnecessary here.
    • Translate your variable names from German ;-)

    Having said this ... here is how I'd do it:

    #!/usr/local/bin/perl -w use strict; sub usage { print << "USAGE"; 'REMoveREMarks'! remrem.pl deletes blank lines or lines starting with a '#' from a give +n file-one and prints it to STDOUT or writes it to file-two Syntax : $0 inputfile # STDOUT, try $0 inputfile | less $0 inputfile outputfile #Output to <outputfile> USAGE exit; } usage() if (!@ARGV or $ARGV[0] eq '--help'); open(FUN,"<$ARGV[0]") || die "Can't open file $ARGV[0] for reading: $! +"; if ($ARGV[1]) { open(GUN,">>$ARGV[1]") || die "Can't open file $ARGV[1] for writing: + $!" } while (<FUN>) { if (!/^#/ and /\w/) { if ($ARGV[1]) { print GUN $_; } else { print "$.: $_" } } } if ($ARGV[1]) { close(GUN) || die "Can't close file $ARGV[1]: $!" } close(FUN) || die "Can't close file $ARGV[0]: $!"

    -- Hofmator