2. Program Code
#!/usr/local/bin/perl
$infile = "foo.txt" ;
$outfile = "bar.txt" ;
$scrapfile = "baz.txt" ;
open(INF,"<$infile") || die "Can't open $infile for reading" ;
open(OUTF,">$outfile") || die "Can't open $outfile for writing" ;
open(SCRAPS,">$scrapfile") || die "Can't open $scrapfile for writing" ;
chop($date = `date`) ; # run system command, remove the newline at the end
foreach $line (<INF>) {
if ($line =~ /IgNore/) {
print SCRAPS $line ;
next;
}
$line =~ s/\*DATE\*/$date/g ;
if ($line =~ /\#/) {
@parts = split ("#", $line);
print OUTF "$parts[0]\n" ;
print SCRAPS "#" . @parts[1..$#parts] ; # range of elements
} else {
print OUTF $line ;
}
}
close INF ; close OUTF ; close SCRAPS ;