#!/usr/bin/perl -w use strict; my @files = qw(file1.dat file2.dat file3.dat); my %hash; foreach my $file (@files) { open (FILE, "<", $file) || die "can't open $file $!"; while () { next if /^\s*$/; #simply skips blank lines chomp; my ($name,@tokens) = split(/,/,$_); $name =~ s/_ref\d+$//; push @{$hash{$name}},@tokens; } } foreach my $name (sort keys %hash) { print "$name @{$hash{$name}}\n"; } __END__ prints: aaa_1 4 5 8 9 bbb 2 3 5 10 ccc_1 5 6 6 11