LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2008, 02:45 AM   #1
johngreg
LQ Newbie
 
Registered: Oct 2008
Posts: 14

Rep: Reputation: 0
need help with perl - pattern matching


i need to write a script to find the count of traces in the log and generate a report.

trace looks something like this...
Code:
[10/10/08 09:12:25:539 CDT]  SEVERE  00000060  efox1dsKSNHRz7_pf4K2_z3  com..billing.UpdateCard
Correlation Identity: 767718e0-96d5-11dd-8f9a-82e948eef517
Additional Data:
        null
Current exception:
Message:
   _ERR_COMMAND_EXCEPTION
Stack trace:
"java.lang.NullPointerException".
        at com.command.ECCommandTarget.executeCommand(ECCommandTarget.java(Compiled Code))
        at com.command.CommandCache.executeCommand(CommandCache.java(Compiled Code))
.
.
End of exception traces.
Text in between 'Stack trace:' and 'End of exception traces.' is one trace.
I tried the below methods, but dint get expected result.
Code:
my $exStart ="Stack trace";
my $exEnd = "End of exception traces.";
1. if(/$exStart/ ... /$exEnd/){
2. split(/^$exStart.*$exEnd/, <FH>);
the problem is, it is getting the text from first 'Stack trace:' to last 'End of exception traces.'

please help
 
Old 10-13-2008, 06:44 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
If I understand correctly:

Code:
perl -nle'
  if (/Stack trace/../End of exception/) {
    /Stack trace/ and ++$c;
    print
    }
  print "Total: $c" if eof' logfile
 
Old 10-13-2008, 07:09 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by johngreg View Post
the problem is, it is getting the text from first 'Stack trace:' to last 'End of exception traces.'
The regular expression engine is greedy by default, meaning it takes as much as it can get. If you say /x.*y/, then you get from 'x' to the last 'y' the engine can find. There's a way to change this behavior, however, by changing '.*' to '.*?'. See perldoc perlretut and look for the description of greedy (maximal) matching vs non-greedy (minimal) matching.

A random example:
Code:
#!/usr/bin/perl
use strict;
use warnings;

while( <DATA> ) {
  m/foo(.*)bar/ and print "Greedy: $1\n";
  m/foo(.*?)bar/ and print "Non-greedy: $1\n";
}

__DATA__
foo 1 2 3 bar foo 4 5 6 bar foo 7 8 9 bar
Which produces this:
Code:
telemachus ~ $ perl greedy 
Greedy:  1 2 3 bar foo 4 5 6 bar foo 7 8 9 
Non-greedy:  1 2 3
 
Old 10-13-2008, 11:05 AM   #4
johngreg
LQ Newbie
 
Registered: Oct 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Question

i was trying the below way, i couldn't get it to work..
Code:
my @chunks = split(m/^$exStart(.*?)$exEnd/, <FH>);
please help
 
Old 10-13-2008, 11:36 AM   #5
johngreg
LQ Newbie
 
Registered: Oct 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

thanks guys.. i got it working like below
Code:
my $exStart ="Stack trace";
my $exEnd = "End of exception traces.";
my $c;
my @traces;
while(<FH>){
    while ( /$exStart(.*?)$exEnd/sgmo ) {
        ++$c;
        push(@traces, $1);
    }
}
i would appreciate any suggestions to improve this.. thanks again

Last edited by johngreg; 10-13-2008 at 11:38 AM.
 
Old 10-13-2008, 01:08 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,657
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
If the regular-expression that you want to match-for is expressed in terms of Perl variables, then you need to tell Perl to do what's known as interpolation. In other words, to first substitute the variables into the string and then use that result as "the pattern to be matched."

Refer to the section, "Regexp-Like Quote Operators" in perldoc perlop.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
irrelevant characters match in PERL pattern matching gaynut Programming 2 08-21-2008 10:52 PM
Perl pattern matching - greedy wondergirl Programming 2 06-17-2008 03:32 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration