473,248 Members | 1,169 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,248 software developers and data experts.

Dynamic Multi-line Pattern Matching

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)

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\)/

Feb 22 '07 #1
8 10557
4,059 Expert 2GB
please post your existing code
Feb 22 '07 #2
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
  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. }
  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. }
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)

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.

Feb 23 '07 #3
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:


# 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

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
Here is a piece of code I picked up from
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.     }
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);
  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. }
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
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. }
where errorpat get set to the different regexps
and so on..
Feb 23 '07 #7
4,059 Expert 2GB
.............. :)
Feb 23 '07 #8
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

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...
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...
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...
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...
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...
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...
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 ...
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...
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.
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.