#!/usr/bin/perl use strict; use warnings; my $file = 'tmpfile'; my $match; my $numelements; open my $info, $file or die "Could not open $file: $!"; while( my $line = <$info>) { if ( $line =~ m/(src\=\d+\.\d+\.\d+\.\d+\sdst\=\d+\.\d+\.\d+\.\d+\ssrc\_port\=\d+\sdst\_port\=\d+).*AGE OUT/ ) { $match = "$1"; push (my @dups, $line); while( my $twoline = <$info> ) { if ( $twoline =~ /$match/ ) { push @dups, $twoline; } } $numelements = @dups; print "$match has $numelements elements\n"; if ( $numelements > 1 ) { print join("\n", @dups); } } } close $info;