#!/usr/bin/perl use strict; use warnings; open my $fh2,"<","file2.txt"; my %hash2; while (my $line=<$fh2>){ chomp $line; my @k = split ',', $line; $hash2{$k[0]} = $k[1]; } close $fh2; open my $fh1,"<","file1.txt"; my @final; while (my $line=<$fh1>){ chomp $line; my @k = split ',', $line; $k[1] = $hash2{$k[0]} if (exists $hash2{$k[0]}); push @final,join(',',@k); } close $fh1; open my $fh3,">","file1.txt"; for my $value (@final) {print $fh3 $value."\n";} close $fh3;