in reply to Re^3: search pattern with digits
in thread search pattern with digits
#!/usr/bin/perl use strict; use warnings; sub search_phrase { my ( $inFile, @phrases ) = @_; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my $line; PHRASE: foreach my $phrase ( @phrases ) { my $rxPhrase = qr{\Q$phrase\E}; # keep reading down the file while ($line = <$inFH>) { # when one phrase matches, jump to the next next PHRASE if $line =~ /$rxPhrase/; } # end of file, and we haven't matched the last phrase return; } # We have just matched the last phrase. # The number we want is somewhere in $line. my ($number) = $line =~ /(\d+)/; return $number; } my $file_n = "test.txt"; my $phrase1 = "Inserted rows"; my $phrase2 = "Requested:"; my $phrase3 = "Applied:"; my $phrase4 = "Rejected:"; my $result = search_phrase($file_n, $phrase1, $phrase2, $phrase3,$phra +se4); if (defined $result) { print "$result\n" } else { print "-1\n" }
This file is to check the number of occurences of the word reject total rows rejected: 80 Inserted rows - Requested: 9642 Applied: 9642 Rejected: 0 this file just contains the phrase reject. reject:3 reject 3 reject 4 total rejected rows: 100 total rejected rows: 90 total rejected rows: 60 total rejected rows:40 total rejected rows:40 90 rows rejected for sub 999 rows rejected for sub 100 rows rejected for sub Reject_Ao total rejected recors: 60 total rejected rows:49 reject:1 390 rows rejected for sub
|
|---|