in reply to Loop problem: jumps one record

Each of the 2 lines where you test against the regex is reading another record. Don't do that. Instead, you could store the record in a variable and then do the two tests on that one variable. eg:

use strict; use warnings; my $ip = ""; while (<>) { if (/Relay.access.denied/) { my $rec; do { $rec = <>; print "$2\n" if $rec =~ /(\d+)\s+(\S+)/; $ip = $2; } until ($rec !~ /(\d+)\s+(\S+)/); } }

However, there are so many other ways to approach this too. It would be better to avoid the regex duplication in the first place, for example.

Addendum: As you appear to be trying to parse the output of pflogsumm, perhaps this is an XY problem?

Replies are listed 'Best First'.
Re^2: Loop problem: jumps one record
by math&ing001 (Novice) on Jan 31, 2017 at 12:05 UTC
    Yes indeed I'm trying to read from pflogsumm's output. The purpose is to read addresses from it and add them to a local database.

      In that case there is a limited number of approaches which you could take to this. I can immediately think of these three.

      1. Use your current strategy to parse the pflogsumm output. This means that you can leverage the options to pflogsumm to help you structure the data in the most useful way to you. Be aware that it is prone to breakage if the output format ever changes.
      2. Modify (a copy of) pflogsumm so that it either outputs precisely what you want or even performs the whole task for you including the database insertion. pflogsumm is written in Perl which makes this option less daunting that it would otherwise be.
      3. Ignore pflogsumm and read the mail log directly. There is Mail::Log::Parse::Postfix which might help with this (I've not used it so YMMV).

      Good luck with your project.