By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

Dynamic Multi-line Pattern Matching

P: 15
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
Feb 22 '07 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
please post your existing code
Feb 22 '07 #2

P: 15
Here is the code.
The usage is
<prog_name>.pl -file <input_file> -pattern_file <input_pattern_file>

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. $debug = 0;
  4. $foundPattern = 0;
  5. while ($arg = shift @ARGV) {
  6.         if ($arg eq "-file") {$file = shift @ARGV;}
  7.         elsif ($arg eq "-pattern_file") {$pattern_file = shift @ARGV;}
  8.         elsif ($arg eq "-debug") {$debug = 1;}
  9. }
  10. if (!defined $file) {
  11.         print "Error: Usage $0 -file <file> -pattern_file <pattern_file>\n";
  12.         exit 1;
  13. }
  14. if (!defined $pattern_file) {
  15.         print "Error: Usage $0 -file <file> -pattern_file <pattern_file>\n";
  16.         exit 1;
  17. }
  18. @errpats = ();
  19. #Get error patterns to search for from pattern_file and store it in @errpats
  20. open (PATFILE, $pattern_file) or die "Error: $file $!\n";
  21. @errPatsFile = <PATFILE>;
  22. chomp(@errPatsFile);
  23. close PATFILE;
  24. foreach $line (@errPatsFile) {
  25.         if ($line =~ /^pattern\s+=\s+(.+?)\s*$/) {
  26.                 push (@errpats, $1);
  27.         }
  28. }
  29. if ($debug == 1) {
  30.         print "NO OF PATTERNS --- $#errpats @errpats\n";
  31. }
  32.  
  33.  
  34. open (INFILE, "$file") or die "Error: $file $!\n";
  35. @fileLines = <INFILE>;
  36. chomp(@fileLines);
  37. close INFILE;
  38. if ($debug ==  1 ) {
  39.         print "FILE SIZE --- $#fileLines\n";
  40. }
  41. $lineNum = 1;
  42. #Foreach line in the file match with all values in errpats.
  43. foreach $fileLine (@fileLines) {
  44.         foreach $errpat (@errpats) {
  45.                 my $ep = "";
  46.                 $cmd = "\$ep = qr$errpat";
  47.                 eval $cmd;
  48.                 if ($debug == 1) {
  49.                         print "CMD --- $cmd\n";
  50.                         print "ERROR PATTERN --- $ep\n";
  51.                 }
  52.                 if ($fileLine =~ $ep) {
  53.                         print "Line: $lineNum Match for $errpat in \n\t $fileLine\n";
  54.                         $foundPattern = 1;
  55.                 }
  56.                 $lineNum++;
  57.         }
  58. }
  59. if ($foundPattern == 1) {
  60.         print "Pattern found in file\n";
  61. } else {
  62.         print "Pattern not found in file=n";
  63. }
  64.  
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
Feb 23 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
See if the second and third FAQs at this link helps you figure out a solution you can incorporate into your existing code:

http://perldoc.perl.org/perlfaq6.html

# 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?
Feb 23 '07 #4

docsnyder
P: 88
@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
Feb 23 '07 #5

P: 15
Here is a piece of code I picked up from
http://perldoc.perl.org/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.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.    undef $/;          
  3.     while ( <> ) {
  4.     while (  /(Error)(.*?)(\(ERR-[0-9]+\))/sgm ) { 
  5.         print "$1\n";
  6.     }
  7.     }
  8.  
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. $file = $ARGV[0];
  3. $pattern_file = $ARGV[1];
  4. open (PATFILE, $pattern_file) or die "Error: $file $!\n";
  5. @errPatsFile = <PATFILE>;
  6. chomp(@errPatsFile);
  7. close PATFILE;
  8. foreach $line (@errPatsFile) {
  9.         print "$line is line\n";
  10.         if ($line =~ /^pattern\s+=\s+(.+?)\s*$/) {
  11.                 push (@errpats, $1);
  12.  
  13.         }
  14. }
  15. undef $/;
  16. open (FILE, "$file") or die "Error: $!\n";
  17. while ( <FILE> ) {
  18.         foreach $errpat (@errpats) {
  19.                 while (  $_ =~ $errpat ) {
  20.                         print "ONE -- $1 ||  TWO -- $2 ||  THREE -- $3\n";
  21.                 }
  22.         }
  23. }
  24.  
  25.  
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
Feb 23 '07 #6

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

KevinADC
Expert 2.5K+
P: 4,059
.............. :)
Feb 23 '07 #8

miller
Expert 100+
P: 1,089
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.
Feb 23 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.