use strict; use warnings; mailog = "/var/log/mail.log"; open (MAIL, "+<$mailog") or die $!; foreach () { chomp; my $datestr =~ (/^.{15}/); my $mailmsg =~ (/^.?connect\sto.*|to=.*|host.*$/); print $datestr,"\n",$mailmsg; } close (MAIL); The output of this code is below and I DO NOT want, can skip the output such as Mar 21 18:58:08 Mar 21 18:58:08 Mar 21 18:58:08 I only want ouput IF the date is followed by to=|connect\sto.*|host.* __OUTPUT__ to=, relay=mx.dca.untd.com[64.136.44.37]:25, delay=177323, delays=177322/0.02/1.8/0, dsn=4.0.0, status=deferred (host mx.dca.untd.com[64.136.44.37] refused to talk to me: 550 Access denied...0b30748490a5c4c48900e4e9912059c0e18d1d24dd45d1896d5150b1f17551f975b124c9b1b12499b11974d1d44099f4d135f440c46d305d...) Mar 21 18:58:08 Mar 21 18:58:08 Mar 21 18:58:08 to=, relay=none, delay=187566, delays=187536/0.05/30/0, dsn=4.4.1, status=deferred (connect to webav.com[74.117.116.83]:25: Operation timed out) Mar 21 18:58:08 to=, relay=none, delay=187566, delays=187535/0.02/30/0, dsn=4.4.1, status=deferred (connect to webav.com[74.117.116.83]:25: Operation timed out) Mar 21 18:58:08 Mar 21 18:58:08 to=, relay=none, delay=187576, delays=187546/0.02/30/0, dsn=4.4.1, status=deferred (connect to mail.phoenixformations.com[69.61.147.50]:25: Operation timed out)