use strict; use warnings; while (){ if (/^Relay access denied/ .. /Sender address rejected/){ print "$2\n" if $_ =~ /(\d+)\s+(\S+)/; } } __DATA__ Relay access denied (total: 2) 1 111.111.111.111 1 222.222.222.222 1 333.333.333.333 Sender address rejected: Access denied (total: 50) 1 200.200.200.200 Relay access denied (total: 1) 1 255.255.255.255 Sender address rejected: Access denied (total: 50) ##OUTPUT 111.111.111.111 222.222.222.222 333.333.333.333 255.255.255.255