use warnings; use strict; use constant { IDLE => 0, IP_ADDRS => 1, }; my $state = IDLE; while (<>) { chomp; if (/Relay.access.denied/) { if ($state == IDLE) { $state = IP_ADDRS } elsif ($state == IP_ADDRS) { warn "unexpected: $_" } } elsif (/(\d+)\s+(\S+)/) { if ($state == IDLE) { warn "unexpected: $_" } elsif ($state == IP_ADDRS) { print "$2\n" } } else { if ($state == IDLE) { } elsif ($state == IP_ADDRS) { $state = IDLE } } }