use warnings; use strict; my $log_file = 'file.log'; my $uid = 'xy1234'; my $threshold = 20; my @occurrences; open my $fh_log, '<', $log_file or die "opening $log_file: $!"; LINE: while (defined(my $line = <$fh_log>)) { next LINE unless $line =~ m{ uid=$uid }xms; push @occurrences, $line; } close $fh_log or die "closing $log_file: $!"; if (@occurrences > $threshold) { print "occurrences for $uid above $threshold \n"; print @occurrences; }