473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Dynamic Multi-line Pattern Matching

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
8 10560
KevinADC
4,059 Expert 2GB
please post your existing code
Feb 22 '07 #2
Natti
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
4,059 Expert 2GB
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
@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
Natti
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
Natti
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
4,059 Expert 2GB
.............. :)
Feb 23 '07 #8
miller
1,089 Expert 1GB
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

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

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
1
by: Silver | last post by:
Hi everyone, my program compiles and executes, but I get an error during run-time. I guess it has something to do with memory allocation (which I don't seem to fully control yet).. Here's the...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
8
by: novus | last post by:
Hi, In ASP.net 2.0 I make a control which add the same controls dynamically. In the oninit event I add the controls to the controls collection. After that the loadviewstate event fills in the...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
0
by: alexandre.bergel | last post by:
Dear colleges, You might want to consider Dyla'07 as a good venue to present your work and your favourite programming language. Regards, Alexandre ...
0
by: Alexandre Bergel | last post by:
Dear colleague, Please, note that after the workshop, best papers will be selected, and a second deadline will then be set regarding preparation of the Electronic Communications of the...
11
by: gianluca | last post by:
Hy list, I've to declare a 3D matrix in C . I tried with that code: double ***mat; mat=(double***)G_malloc(ndimension*(sizeof(double)); but the program send me a run time error.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.