use strict; use warnings; my $file1 = <) { chomp; my ($key, @tail) = split ','; next if ! @tail; $concat{$key} = \@tail; } close $file1In; open my $file2In, '<', \$file2 or die "Failed to open file2: $!"; while (<$file2In>) { chomp; my ($key, @tail) = split ','; next if ! @tail or $key !~ s/_(\w*)$// or $1 ne 'rf1'; push @{$concat{$key}}, @tail; print join (',', $key, @{$concat{$key}}), "\n"; } close $file2In;