open my $tailf, "tail -f $logFile |" or die; while (<$tailf>) { chomp; my $line = $_; if($line =~ m/ on $ipaddress/) { if (scalar(@matches) <= 2){ push(@matches, $line); } else { last; } } } foreach $line (@matches){ print "$line\n"; } close $tailf;