You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2 KiB

#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use English qw/$PROGRAM_NAME/;
my $basename = basename($0);
my $USAGE = <<USAGE
Usage: $basename <infile> <outfile>
USAGE
;
sub show_last_chapter_with_nlines {
my ($ch, $nlines) = @_;
print "Chapter $ch has $nlines lines\n";
}
sub main {
open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!";
open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!";
my $have_seen_firstchapter = 'no';
my $nlines = 0;
my $chline = '';
my $ch = '';
foreach my $inline (<INFILE>) {
if ($inline =~ /^\s*CHAPTER.*$/) {
$chline = "$inline";
$ch = "$chline";
$ch =~ s/^\s*CHAPTER\s*([0-9]*).*/$1/;
$ch =~ s/[\r\n]//;
if ($have_seen_firstchapter eq 'yes') {
show_last_chapter_with_nlines($ch - 1, $nlines);
$nlines = 0;
} else {
$have_seen_firstchapter = 'yes';
$nlines = 0;
}
print OUTFILE $inline;
}
$nlines++;
}
show_last_chapter_with_nlines($ch, $nlines);
}
if ($PROGRAM_NAME eq __FILE__) {
main();
}
1;
__END__