in reply to File read and strip

Fix: (one-liner) ( Added missing >. Thanks Hofmator. )

perl -ne "print unless /^#/" infile > outfile

Fix: (program -- memory efficient)

use strict; use warnings; die("usage: $0 {infile} {outfile}\n"; if @ARGV != 2; my ($file_in, $file_out) = @ARGV; open(my $fh_in, '<', $file_in) or die("Unable to open input file \"$file_in\": $!\n"); open(my $fh_out, '>', $file_out) or die("Unable to create output file \"$file_out\": $!\n"); while (<$fh_in>) { if (!/^#/) { print $fh_out $_; } }

Fix: (program -- (more) compact)

use strict; use warnings; die("usage: $0 {filein} {fileout}\n"; if @ARGV != 2; my ($file_in, $file_out) = @ARGV; open(my $fh_in, '<', $file_in) or die("Unable to open input file \"$file_in\": $!\n"); open(my $fh_out, '>', $file_out) or die("Unable to create output file \"$file_out\": $!\n"); print $fh_out (grep !/^#/, <$fh_in>);

Replies are listed 'Best First'.
Re^2: File read and strip
by Hofmator (Curate) on Nov 15, 2006 at 07:18 UTC
    Fix: (one-liner)

    Should be perl -ne "print unless /^#/" infile > outfile. Apart from that all very sound advice, ikegami++!

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.