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