Dynamic Multi-line Pattern Matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Natti
    New Member
    • Feb 2007
    • 15

    Dynamic Multi-line Pattern Matching

    Hello,
    I have a perl program which parses an input file for specific error patterns and generates an output file with all these lines.

    One of the error patterns that I am looking at spans across multiple lines. I can detect it can as error pattern using information from the first line but cannot print out the remaining lines.
    Is there a way to regexp multiple lines and store all lines in a string or array.


    For eg an error pattern would be
    Error: Module has the following
    1. no link
    2. internal wiring
    issues. Use check_module on module top to resolve (ERR-30)

    or
    Error: Unable to link design (ERR-200)

    In the latter case I just do a regexp for m/^Error:.*\(ERR-200\)/ and write out the line to my output file.
    In the former I can write out only the first line.

    How can I capture all four lines. Remember the no of lines may vary.
    Is there a regular expression that can capture
    m/^Error(any number of new lines)\(ERR-30\)/


    Thanks
    Natti
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    please post your existing code

    Comment

    • Natti
      New Member
      • Feb 2007
      • 15

      #3
      Here is the code.
      The usage is
      <prog_name>.p l -file <input_file> -pattern_file <input_pattern_ file>

      Code:
      #!/usr/bin/perl -w
      
      $debug = 0;
      $foundPattern = 0;
      while ($arg = shift @ARGV) {
              if ($arg eq "-file") {$file = shift @ARGV;}
              elsif ($arg eq "-pattern_file") {$pattern_file = shift @ARGV;}
              elsif ($arg eq "-debug") {$debug = 1;}
      }
      if (!defined $file) {
              print "Error: Usage $0 -file <file> -pattern_file <pattern_file>\n";
              exit 1;
      }
      if (!defined $pattern_file) {
              print "Error: Usage $0 -file <file> -pattern_file <pattern_file>\n";
              exit 1;
      }
      @errpats = ();
      #Get error patterns to search for from pattern_file and store it in @errpats
      open (PATFILE, $pattern_file) or die "Error: $file $!\n";
      @errPatsFile = <PATFILE>;
      chomp(@errPatsFile);
      close PATFILE;
      foreach $line (@errPatsFile) {
              if ($line =~ /^pattern\s+=\s+(.+?)\s*$/) {
                      push (@errpats, $1);
              }
      }
      if ($debug == 1) {
              print "NO OF PATTERNS --- $#errpats @errpats\n";
      }
      
      
      open (INFILE, "$file") or die "Error: $file $!\n";
      @fileLines = <INFILE>;
      chomp(@fileLines);
      close INFILE;
      if ($debug ==  1 ) {
              print "FILE SIZE --- $#fileLines\n";
      }
      $lineNum = 1;
      #Foreach line in the file match with all values in errpats.
      foreach $fileLine (@fileLines) {
              foreach $errpat (@errpats) {
                      my $ep = "";
                      $cmd = "\$ep = qr$errpat";
                      eval $cmd;
                      if ($debug == 1) {
                              print "CMD --- $cmd\n";
                              print "ERROR PATTERN --- $ep\n";
                      }
                      if ($fileLine =~ $ep) {
                              print "Line: $lineNum Match for $errpat in \n\t $fileLine\n";
                              $foundPattern = 1;
                      }
                      $lineNum++;
              }
      }
      if ($foundPattern == 1) {
              print "Pattern found in file\n";
      } else {
              print "Pattern not found in file=n";
      }
      An example of pattern file is given below
      pattern = /^Error.*(\w+-\d+)/
      pattern = /^ABCD$/


      An example of the file in which the patterns are present is given below
      Warning: ABCDEFG (WARN-294)
      Error: John Doe is not in Fountainhead (ERR-102)
      Error: Windows works fine (ERR-204)
      Error: All work and no play
      makes jack a dull boy (ERR- 203)
      Error: ABCDEFG HIJKLMNOP (ERR-104)
      ABCD



      I realised that I was doing a line by line checking for error pattern check and this will not help in multiple line search anyway.
      Is there a way to extend this to search for patterns spanning multiple lines in a file.

      Thanks,
      Natti

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        See if the second and third FAQs at this link helps you figure out a solution you can incorporate into your existing code:



        # I'm having trouble matching over more than one line. What's wrong?
        # How can I pull out lines between two patterns that are themselves on different lines?

        Comment

        • docsnyder
          New Member
          • Dec 2006
          • 88

          #5
          @Natti

          How to solve your problem depends on your knowledge about the error messages.

          If you are concious about all possible errors, you could perform error specific parsing, because you then know which line is the last one for a specific error.

          If you do not know about which error messages are around and how many lines would belong to them, I hardly see a chance to handle them the way you desire.

          Reading in an entire file into a single string does not really solve your problem, because you then still have to separate individual (original) lines.

          Greetz, Doc

          Comment

          • Natti
            New Member
            • Feb 2007
            • 15

            #6
            Here is a piece of code I picked up from
            http://perldoc.perl.or g/perlfaq6.html#I 'm-having-trouble-matching-over-more-than-one-line.
            --What's-wrong%3f-regex%2c-multiline-regexp%2c-multiline-regular-expression%2c-multiline
            and modified it to suit my requirements.


            Code:
            #!/usr/bin/perl -w
               undef $/;  		
                while ( <> ) {
            	while (  /(Error)(.*?)(\(ERR-[0-9]+\))/sgm ) { 
            	    print "$1\n";
            	}
                }
            and this works. But one of my problems is that I do not have this pattern only. I have multiple patterns I need to search for. I have a file full of patterns that need to be looked at and pass it on to the parser. However, when I use the variable that contains the pattern, my parser does not work. I am posting the corresponding code below.

            Code:
            #!/usr/bin/perl -w
            $file = $ARGV[0];
            $pattern_file = $ARGV[1];
            open (PATFILE, $pattern_file) or die "Error: $file $!\n";
            @errPatsFile = <PATFILE>;
            chomp(@errPatsFile);
            close PATFILE;
            foreach $line (@errPatsFile) {
                    print "$line is line\n";
                    if ($line =~ /^pattern\s+=\s+(.+?)\s*$/) {
                            push (@errpats, $1);
            
                    }
            }
            undef $/;
            open (FILE, "$file") or die "Error: $!\n";
            while ( <FILE> ) {
                    foreach $errpat (@errpats) {
                            while (  $_ =~ $errpat ) {
                                    print "ONE -- $1 ||  TWO -- $2 ||  THREE -- $3\n";
                            }
                    }
            }
            My pattern file that I pass in as ARGV[1] is as follows,
            pattern = /(Error)(.*?)(\( ERR-[0-9]+\))/sgm
            pattern = /(Warning)(.*?)( \(ERR-[0-9]+\))/sgm


            Essentially the part
            foreach $errpat (@errpats) {
            while ($_ =~ $errpat ) { etc does not work. I also tried
            while(eval "\$_ =~ $errpat" ) { which also does not work
            }

            Can I get some help on making this pattern match work using the variable $errpat

            Comment

            • Natti
              New Member
              • Feb 2007
              • 15

              #7
              It works now.
              Here's how I have used the errpat variable.
              Code:
              while (  $_ =~ m/$errpat/sgm ) {
                             print "$1  $2 $3\n";
              }
              where errorpat get set to the different regexps
              ^(Error)(.*?)(\ (ERR-[0-9]+\))
              and so on..

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                .............. :)

                Comment

                • miller
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1086

                  #9
                  Originally posted by Natti
                  It works now.
                  Excellent job Natti. That's exactly the regex I was going to suggest to you.

                  It's actually a rather typical problem, a known beginning and ending with non-greedy matching in the middle. The biggest challenge is the multiline modifiers, which is why the faq that you learned from is always a good resource to remember.

                  Comment

                  Working...