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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.