$ ./11148698-00.pl id name 123 john 11 peter 87 helen #### #!/usr/bin/env perl use strict; use warnings; use utf8; my @data = ( q/id name/, q/123 john/, q/34 john/, q/567 john/, q/11 peter/, q/899 peter/, q/87 helen/, ); my %seen; while ( my $str = shift @data ) { chomp $str; my @part = split /\s+/, $str, 2; if ( not exists $seen{ $part[1] } ) { print $str, qq{\n}; } $seen{ $part[1] }++; } print qq{\n};