use strict; my ( $file1, $file2 ) = @ARGV; # (getting file names from command line is better than hard-coding them) # read file2 first, to get the keys and data to test against my %refdata; open( F, $file2 ) or die "$file2: $!"; while () { my ( $key, $data ) = split( /,/, $_, 2 ); # (in case key is not 4 digits) $refdata{$key} = $data; } # now read file1, and output lines that meet the spec open( F, $file1 ) or die "$file1: $!"; while () { my ( $key, $data ) = split( /,/, $_, 2 ); print if ( !exists( $refdata{$key} ) or $data eq $refdata{$key} ); } # (use the command line to redirect output to a "final" file -- e.g.: # # shell> perl your_script file1 file2 > final # # again, it's better than hard-coding another file name