Hello Sheen,

Perl could solve this problem with code like that below. Notice the $trie, (pronounced 'try'), variable. Starting with perl 5.10 I believe, perl uses a trie to search for alternating strings. It is Big O1 or constant and scales well.

My code builds a trie of the alternating values in file1. Then, it reads file 2 and if the beginning of any line matches the trie, it prints out that line from file 2. If you want that in a third file, simply open a file for wring and print there. My example just prints to STDOUT, (the console window).

Chris

Code:
#!/usr/bin/perl
use strict;
use warnings;
use 5.014;

my $file1 = <<EOF;
1823
607
R2A9
802
771
EOF

my $file2 = <<EOF;
1823 1 2 4
22 11 4 29
607 12 3 3
R2A9 34 4 9
D33 2 1 0
802 30 8 1
771 3 0 9
3RE 6 3 1
EOF

my $trie;
{
	local $/;
	open my $fh, "<", \$file1;
	$trie = join "|", split /\n/, <$fh>;
	close $fh or die $!;
}

open my $fh, "<", \$file2;
/^(?:$trie)/ && print  while <$fh>;
close $fh or die $!;
The output is:

Code:
C:\Old_Data\perlp>perl t.pl
1823 1 2 4
607 12 3 3
R2A9 34 4 9
802 30 8 1
771 3 0 9