in reply to SOLVED!!!: search contents of one file and replace with contents of another to make a new file

You can store your file2 names and ids in a hash, then loop through file1 and make substitutions:
use warnings; use strict; my $f1 = '1.txt'; # set up files 1 through 3 my $f2 = '2.txt'; my $f3 = '3.txt'; 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>) { for my $name (keys %ids) { s/$name/${name}_$ids{$name}/; } print $fho $_; } close $fh; close $fho;
  • Comment on Re: search contents of one file and replace with contents of another to make a new file
  • Download Code