use strict; use warnings; my $previous = ''; LINE: while(my $line = ) { # next LINE unless length $line >= 11; ## useless if you're absolutely sure that you can't have a shorter line # next LINE unless $line =~ m<\S>; ## Checking if the line isn't blank, same as above my $var = substr($line, 9, 2); next LINE if $var eq $previous; # with eq or ne instead of == or != this works even for hexadecimal values, or any string of two characters # At this point, lines with the same number as the previous have been skipped $previous = $var; print $line; } __DATA__ AB000000026JHAHKDFK AB000000028JHKHKHKJ AB00000003033AFSFAS AB000000030HJHKH80J AB000000030LOIKJUJ8 AB0000000324446KJHK #### AB000000026JHAHKDFK AB000000028JHKHKHKJ AB00000003033AFSFAS AB0000000324446KJHK