#!/usr/bin/perl use strict; use warnings; my $file_1 = $ARGV[0] || 'file1.txt'; my $file_2 = $ARGV[1] || 'file2.txt'; open (FILE1, '<', $file_1) or die "Unable to open $file_1 for reading : $!"; open (FILE2, '<', $file_2) or die "Unable to open $file_2 for reading : $!"; my %offset = ( _pos => 0 ); while ( ) { chomp; if ( defined $offset{ $_ } ) { seek FILE2, $offset{ $_ }, 0; print scalar ; next; } else { seek FILE2, $offset{_pos}, 0; my $pos = tell FILE2; while ( my $line = ) { my ($col1) = $line =~ /^(\d+)/; $offset{ $col1 } = $pos; $pos = tell FILE2; if ( $col1 eq $_ ) { print $line; $offset{_pos} = $pos; last; } } } }