in reply to Get data from a file to search and replace within a second file

I took the freedom to slightly simplify your spec, using a lookup table. The output should be what I suppose you want, i.e. replace all occurrences of c1 etc. with c1 d1 etc.:

#!/usr/bin/perl use strict; use warnings; my $fname_A = "file_A.txt"; my $fname_B = "file_B.txt"; my $fname_B_out = "file_B_out.txt"; my %subst; # lookup table open (my $data_fh, "<", $fname_A) or die "Couldn't open '$fname_A': $! +"; while (<$data_fh>) { chomp; my ($find, $add) = (split /\t/)[2,3]; $subst{$find} = $add; # print "$find => $add\n"; # debug } my $search = join "|", map quotemeta, keys %subst; open (my $in_fh, "<", $fname_B) or die "Couldn't open '$fname_B': + $!"; open (my $out_fh, ">", $fname_B_out) or die "Couldn't open '$fname_B_o +ut' for writing: $!"; while (my $line = <$in_fh>) { $line =~ s/($search)/$1 $subst{$1}/g; print $out_fh $line; }

The output might not be what you want in case the search strings aren't unique (because of the lookup hash), or if the substitutions aren't independent, such as when c1 => c2 in the first run through the file, c2 => d2 in the second run, etc. (because of doing it all in one go).