#!/usr/bin/perl # use strict; use warnings; use List::Util q{first}; sub search_phrase { my @array; my ($inFile, @phrases) = @_; 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 "-1"; # print qq{$phrase: not found in sequence\n}; next; } print "" if ($lines[$lineNo] =~ m{\Q$lastPhrase\E\s*(\d*)}); push (@array, $1); $lineNo++; splice @lines, 0, $lineNo; } return (@array); } my $file_n = "665172.data"; my $phrase1 = "total rejected rows:"; my $phrase2 = "total rejected recors:"; my $phrase3 = "rejectb:"; my $phrase4 = "total rejected rows:"; my @newarray = search_phrase( $file_n, $phrase1, $phrase2, $phrase3, $phrase4 ); my $count = ($#newarray); $newarray[$count] =~ s/\s+//g; if ((($#newarray + 1) >= 1) && ($newarray[$count] gt 0)) { print "$newarray[$count]\n"; } else { print "-1\n"; } #### foreach my $thisLine (<$inFH>) { .. do something with $thisLine .. } #### while (<$inFH>) { .. do something with $thisLine .. }