use strict; use warnings; my $prev; while () { chomp; # Note that $prev will be undef only on the first time round. my $curr = $_; if (!defined $prev) { $prev = $curr; next; } if ($curr ne $prev) { print "$prev\n"; $prev = $curr; } else { undef $prev; } } print "$prev\n" if defined $prev; __DATA__ a1a a1a b1b c1c c1c d1d d1d e1e f1f g1g g1g h1h h1h i1i j1j