20070702 strip comments from a file - plembo/onemoretech GitHub Wiki

title: strip comments from a file link: https://onemoretech.wordpress.com/2007/07/02/strip-comments-from-a-file/ author: lembobro description: post_id: 677 created: 2007/07/02 16:19:00 created_gmt: 2007/07/02 16:19:00 comment_status: open post_name: strip-comments-from-a-file status: publish post_type: post

strip comments from a file

Another oldie but goodie from Gary Utz:

#!/usr/bin/perl -w
$infile = $ARGV[0];
$outfile = $ARGV[1];

open I, $infile or die("nCannot open input file $!n");
@file = _;close(I);
foreach $line(@file) {
    $firstchar = substr($line,0,1);
    $firsttwo = substr($line,0,2);
    
    if ($firsttwo ne "#!" &&  $firstchar eq "#") { next; }
    elsif ($firstchar eq "n") { next; }
    else {push @output, $line; }
 }

open O, "> $outfile" or die("nCannot open output file $!n");
print O @output;
close(O);

He is amazing, isn't he?

Copyright 2004-2019 Phil Lembo