my %seen; while () { print "$1\n" if /(\d+)$/ and not $seen{$1}++; } __DATA__ foo 1 foo 2 bar 1 bar 3 bar 4 bar 2 bar 2 bar 1 baz 1 ex 5