$ cat spw663850.dat
total Laptops produced: 60
total cpu sold: 57
total mice produced: 40
total cpu sold: 45
total Laptops produced: 68
total mice produced: 48
total cpu sold: 51
total printers produced: 19
total monitors produced: 149
$
$
$ cat spw663850
#!/usr/bin/perl
#
use strict;
use warnings;
use List::Util q{first};
my ( $inFile, @phrases ) = @ARGV;
my $lastPhrase = $phrases[ -1 ];
open my $inFH, q{<}, $inFile
or die qq{open: $inFile: $!\n};
my @lines = <$inFH>;
close $inFH or die qq{close: $!\n};
foreach my $phrase ( @phrases )
{
my $rxPhrase = qr{\Q$phrase\E};
my $lineNo =
first { $lines[ $_ ] =~ $rxPhrase } 0 .. $#lines;
unless ( defined $lineNo )
{
print qq{$phrase: not found in sequence\n};
next;
}
print qq{$1\n}
if $lines[ $lineNo ] =~ m{\Q$lastPhrase\E\s*(\d+)};
$lineNo ++;
splice @lines, 0, $lineNo;
}
$
$
$ ./spw663850 spw663850.dat 'total Lap tops produced:' 'total mice pro
+duced:' 'total cpu sold:'
45
$
Feel free to ask if the script is not clear. I cleaned up the data file so that none of the lines had spaced before the colons just for consistency. Cheers,
JohnGG |