in reply to Re: Searching pattern in 400 files and getting count out of each file
in thread Searching pattern in 400 files and getting count out of each file
open my $p, "< ". shift @ARGV or die "could not open pattern file: $!"; my @patterns = <$p>; chomp @patterns; for my $filename (@ARGV) { open my $f, "< $filename" or die "could not open $filename: $!"; my @lines = <$f>; for my $pattern (@patterns) { printf "%s has %d matches for pattern /%s/\n", $filename, scalar(grep /$pattern/, @lines), $pattern; } }
Going back to the original pulling patterns from a db gives us:
# get column 0 from all result rows simultaneously.... my $results = $sth->fetchall_arrayref([0]); # error check here? # results are ref to array of hashes, so convert to array # we could avoid this and use "results" directly my @patterns = map { $_->[0] } @$results; for my $filename (@ARGV) { open my $f, "< $filename" or die "could not open $filename: $!"; my @lines = <$f>; for my $pattern (@patterns) { printf "%s has %d matches for pattern /%s/\n", $filename, scalar(grep /$pattern/, @lines), $pattern; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Searching pattern in 400 files and getting count out of each file
by Rita_G (Initiate) on Nov 09, 2012 at 06:52 UTC | |
by Athanasius (Archbishop) on Nov 09, 2012 at 12:52 UTC |