use warnings; use strict; my $f1 = '1.txt'; # file that needs the treatments added on my $f2 = '2.txt'; # file that has all of the treatments and an underscore my $f3 = '3.txt'; # output file my %ids; my $fh; open $fh, '<', $f2 or die; while (<$fh>) { chomp; my ($name, $id) = split /_/; $ids{$name} = $id; } close $fh; open my $fho, '>', $f3 or die; open $fh, '<', $f1 or die; while (<$fh>) { chomp; my @names = split /,/; foreach my $name (@names) { $name = $name . '_' . $ids{$name}; } my $line = join(',', @names); print $fho $line, "\n"; } close $fh; close $fho;