I have one pat_array, that has the user given patterns and contains special characters. Let's say "Error 78?" "(Not available) 77%" etc. I need to match the patterns in the 1GB+ log file, after a particular line number till end. I also need to keep a count of which pattern found how many times. This code below works but it takes long, close to 2 mins, for 3 patterns. I was thinking of a way, to avoid the extra for loop while matching the patterns and do it in one shot. (Here for 3 patterns, I match $_ with three different patterns from pat_array, as you can see). I am really really new to perl, so please explain when you help me with the new code/idea.
my @pat_array = split('@@@', $InListOfPatterns); my $num_pat = @pat_array; my @match_count; for( my $i = 0; $i < $num_pat; $i = $i + 1 ) { $match_count[$i] = 0; } open LOG_READ,'<',"$InLogFilePath" || die "can not open file :$!"; while (<LOG_READ>) { chomp; if ( $. > $InStartLineNumber) { for(my $j = 0; $j < $num_pat; $j = $j + 1 ) { if ($_ =~ m/\Q$pat_array[$j]\E/){ $match_count[$j] = ($match_count[$j] + 1); }}}} close (LOG_READ);
Thanks in advance. :)
In reply to Multiple patterns match in a big file and track the counts of each pattern matched by ansh007
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |