in reply to Regex perl grep usage string match comparison

  1. Always use warnings; use strict; !
  2. This is not such a big issue but it's considered better and safer to use the 3-arg open and lexical filehandles, e.g. open my $fh, '<', $infile or die "Could not open $infile: $!";
  3. Problem: This: $/ = "#>\n"; sets the input record separator globally from that point forward, including the point in the current code where you're reading the configuration file. That means that currently, <CONFIG> will read the entire file at once. You should at least use local $/ = "#>\n"; in a new block and follow the next tip:
  4. Parsing the whole configuration file for every line of input is inefficient; instead read the configuration file once at the beginning of the script and store its values in a data structure such as an array of arrays for later use. This also helps avoid the previous problem of the input record separator.
  5. The configuration file format is unclear and the loop for $checkItem (@configLineItems) doesn't make much sense to me. This is probably also because your sample configuration file "patterns" don't match any of the messages from the sample log file. I'm going to wager a guess that the format of the configuration lines is: "Message::Pattern::Pattern" etc. In that case I would suggest that you shift the first item off the array before the aforementioned loop, as in my $message = shift @configLineItems; and then use $message instead of $configLineItems[0].
  6. Instead of "$Line[6]" =~ $checkItem, this is better: $Line[6] =~ /\Q$checkItem/ (however if $checkItem contains actual regular expressions, drop the \Q) - see perlre for more information. The anchors ^ and $ as well as the modifier /i may be of interest to you.

Please try these suggestions, and if you have further questions, please provide the updated code and better sample input (both logfile and configuration file), as well as the desired output.

Replies are listed 'Best First'.
Re^2: Regex perl grep usage string match comparison
by justinkala (Initiate) on Aug 06, 2014 at 13:31 UTC
    Thank you very much for the suggestions. I am new to Perl.Can you provide me the code (if you can)..from the files I provided??that will be great and helpful..
      Did you read the post you replied to? It says the specifications are unclear, that needs some clarification from you. Also, PerlMonks is here to help people learn Perl. To get help, please show some effort (try to apply the suggestions you've been given, write some code, perlintro) - or you can pay someone to write the code for you (not here).