while (<>){ if (/^Relay access denied/){ foreach (<>){ print "$2\n" if $_ =~ /(\d+)\s+(\S+)/; } } }