#!/usr/bin/env perl use strict; use warnings; use File::Basename; use English qw/$PROGRAM_NAME/; my $basename = basename($0); my $USAGE = < 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 () { 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__