#! perl -slw use strict; our $N //= 1e6; my $file1 = 'file1'; my $spill = 'temp'; open FILE2, '<', 'file2' or die $!; my $done = 0; until( $done ) { ## Index a batch of records from file 2 my %idx; for( 1 .. $N ) { chomp( my @bits = split /\*/, ); $idx{ $bits[ 0 ] } = $bits[ 1 ]; $done = 1, last if eof FILE2; } open FILE1, '<', $file1 or die $!; open SPILL, '>', $spill or die $!; ## process records from file (or spill) file while( ) { chomp( my @bits = split /\*/ ); ## If it exists in this batch, write the modified record to STDOUT if( exists $idx{ $bits[ 0 ] } ) { print join '*', $bits[ 0 ], $idx{ $bits[ 0 ] }, $bits[ 2 ]; } else { ## otherwise put it in the spill file for next pass local $\; print SPILL; } } close SPILL; close FILE1; ## switch the input(file1) and spill file names ( $file1, $spill ) = ( $spill, $file1 ) unless $done; } ## Anything left in the spill file goes through unmodified local $\; open SPILL, '<', $spill; print while ; close SPILL; ## Remove the tempories unlink $spill; unlink $file1;