#!/usr/bin/perl -w use strict; open FIRST,"file1" or die "Can't open file1: $!\n"; open LAST,"file2" or die "Can't open file2: $!\n"; my ( $Flag, @Last, @First, %Hash ); chomp ( @First = ); chomp ( @Last= ) ; $Flag = 0; foreach my $record ( @First ) { $record =~/^(\S*) (\d*)$/; $Hash{$1} = $2 ; } foreach my $record ( @Last ) { $record =~ /^(\S*) (\d*)$/; unless ( exists $Hash{$1} ) { $Flag++; $Hash{$1} = $2; } } if ( $Flag ) # This condition checking avoids unwanted creation of the new file. Hope it will add value to the code. { open NEW,">file3" or die "Can't open file3: $!\n"; map { print NEW "$_ $Hash{$_}\n"} sort keys %Hash ; close NEW; } close FIRST; close LAST;