#!/usr/bin/perl -w use strict; my @files = qw(file1.dat file2.dat); my %hash; foreach my $file (@files) { open (FILE, "<", $file) || die "can't open $file $!"; while () { next if /^\s*$/; chomp; my ($name,@tokens) = split(/,/,$_); $name =~ s/_.*$//; #delete trailing _blah in name push @{$hash{$name}},@tokens; } } foreach my $name (sort keys %hash) { print "$name @{$hash{$name}}\n"; } __END__ prints: Bbbb 3333 4444 23 45 Cdd 3444 3444 24.45 Zddd 345 456 34 456.4 file1.dat: Bbbb,3333,4444 Cdd,3444,3444 Zddd,345,456 file2.dat Bbbb_rf1,23,45 Cdd_rf2,24.45 Zddd,34,456.4