Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

only print data ie between two lines

by namishtiwari (Acolyte)
on Jun 17, 2009 at 05:53 UTC ( #772235=perlquestion: print w/replies, xml ) Need Help??
namishtiwari has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here is my code snippet
#!/usr/bin/perl my $logFile = $ARGV[0]; die "usage: $0 <logFile>" unless $logFile; die "Logfile $logFile doesn't exist" unless -f "$logFile"; open($log, "<", $logFile) or die "Can't open $logFile for reading."; print "Processing file $logFile...\n"; my $n = 0; my $ArcotIDError_Count = 0; my $QnAError_Count = 0; my $UPError_Count = 0; my $OTPError_Count = 0; my $errorCode; while( $line = <$log> ) { $n++; $line =~ tr/\r\n//d; if ($line =~ /recvd AA_BIN_MSG_VER_CHG/) { while( $line = <$log> ) { if($line =~ /Handling NSPAdvice for mechanism \[4\]/) { while ( $line = <$log> ) { if ($line =~ /Authentication mechanism returne +d \[(\d+)\]/) { my $errorCode = $1; print "$errorCode\n"; if ($errorCode != 0 && $errorCode != 1) { $ArcotIDError_Count++; } } } } } } } print "Total Number Of ArcotID Authentication ErrorCode returned i +s $ArcotIDError_Count\n";
This code does not give me correct output. Correct output according to my logfile is 142 but it gives me 352. I want to count it in between these lines
starting point- Apricot Native Server: recvd AA_BIN_MSG_VER_CHG and ending point- (Prepared to Send OK) OR (Sending Invalid credential).
I can check the begining statement here but for end statement(so that i always get the out between those lines) what change needed in the code. Thanks NT

Replies are listed 'Best First'.
Re: only print data ie between two lines
by Corion (Pope) on Jun 17, 2009 at 06:57 UTC
Re: only print data ie between two lines
by citromatik (Curate) on Jun 17, 2009 at 10:08 UTC

    All those while ( $line = <$log> ) seems to me a bad design of your algorithm. If any of the ifs inside them fails, you will consume all the input in a inner while. It seems that at some point one of your regular expressions inside and if statement is failing, thus consuming the rest of the fail. I would also recommend you to use strict and use warnings in your code, specially in code you are testing and (like this case) is giving problems.

    I want to count it in between these lines...

    The range operator (see perlop) could be an alternative for this. Something like (untested):

    while (my $line = <$log>){ if (/recvd AA_BIN_MSG_VER_CHG/ .. /?:(Prepared to Send OK|Sending I +nvalid credential)/){ if (/Handling NSPAdvice for mechanism \[4\]/ .. /whatever_condit +ion_to_reset_this_block/) { if ($line =~ /Authentication mechanism returne +d \[(\d+)\]/) { my $errorCode = $1; print "$errorCode\n"; $ArcotIDError_Count++ if ($errorCode != 0 && $errorCode ! += 1); } } } }


      i got this error message when i ran the code with range operator-
      Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HER +E :(Prepar ed to Send OK|Sending Invalid credential)/ at line +34.
      Thanks NT

        My mistake:

        /?:(Prepared to Send OK|Sending I +nvalid credential)/
        should be
        /(?:Prepared to Send OK|Sending I +nvalid credential)/


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://772235]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2016-09-28 08:41 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (523 votes). Check out past polls.