my $last_line; while (<$fh>) { chomp; # Skip duplicates next if $.>1 && $_ eq $last_line; $last_line = $_; ...proceed as normal... }