use Modern::Perl; my $lastrow = ""; open my $fh, '<', 'fail.txt' or die $!; while (my $line = <$fh>) { $line =~ s/\n//g; next if !$line; if ($line ne $lastrow) { print $line, "\n\n"; $lastrow = $line; } } close $fh; #### hostname1.com Gateway FAIL hostname2.com Gateway FAIL