#!/usr/bin/perl use strict; use Data::Dumper; my %file2 = (); open FILE2,'<',"./b.txt" or die "Cannot open b.txt"; while (){ my @words = split /\s+/,$_; my $zz = join 'zz',@words; for (@words){ $file2{$_} = $zz; } } close FILE2; print Dumper \%file2; open FILE1,'<',"./a.txt" or die "Cannot open a.txt"; open FILE3,'>',"./r.txt" or die "Cannot create r.txt"; while (){ chomp; my @words = split /[ \.]/,$_; my @added; for (@words){ push @added,$file2{$_} if exists $file2{$_}; }; print FILE3 $_,(join ' ',@added),"\n"; } close FILE1; close FILE3;