#!/usr/bin/perl -w use strict; use warnings; use diagnostics; my %fn=(); while (my $fnam=) { chomp $fnam; next unless $fnam =~ m@ ^( # Begin capture $1 [a-zA-Z]+ # One or more alphas \_ # followed by underscore \d+ # one or more digits ) # End capture $1 .*$ # Who cares about the rest @x; $fn{$1}++; } printf "%s\n",join("\n",sort keys %fn); exit(0); __END__ smith_13_503_de7.p smith_13_502_de7.v jones_104_503_de7.p jones_104_502_de7.v #### --$ perl fnamParse.pl jones_104 smith_13 --$