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