简体   繁体   中英

Comparing Two text files in perl and output the matched result

I want to compare two text files that i have generated from one of the perl script that i wrote. I want to print out the matched results from those two text files. I tried looking at couple of answers and questions that people have asked on stackoverflow but it does not work for me. Here is what i have tried.

my $file1 = "Scan1.txt";
my $file2 = "Scan2.txt";
my $OUTPUT = "final_result.txt";
my %results = (); 
open FILE1, "$file1" or die "Could not open $file1 \n";
   while(my $matchLine = <FILE1>)
       {   
         $results{$matchLine} = 1;
    }
    close(FILE1); 
    open FILE2, "$file2" or die "Could not open $file2 \n";
   while(my $matchLine =<FILE2>) 
        {  
    $results{$matchLine}++;
        }
    close(FILE2);  
    open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
    foreach my $matchLine (keys %results) { 
    print OUTPUT $matchLine if $results{$matchLine} ne 1;
    }
    close OUTPUT;

EXAPLE OF OUTPUT THAT I WANT

FILE1.TXT data 1 data 2 data 3

FILE2.TXT data2 data1

OUTPUT data 1 data 2

Your problem is that your hash now has following states:

  • 0 (line not found anywhere),
  • 1 (line found in file1 OR line found once in file2),
  • 2 (line found in file1 and once in file2, OR line found twice in file2)
  • n (line found in file1 and n-1 times in file2, OR line found n times in file2)

This ambiguity will make your check (hash ne 1) fail.

The minimal required change to your algorithm would be:

my $file1 = "Scan1.txt";
my $file2 = "Scan2.txt";
my $OUTPUT = "final_result.txt";
my %results = (); 
open FILE1, "$file1" or die "Could not open $file1 \n";
   while(my $matchLine = <FILE1>)
       {   
         $results{$matchLine} = 1;
    }
    close(FILE1); 
    open FILE2, "$file2" or die "Could not open $file2 \n";
   while(my $matchLine =<FILE2>) 
        {  
    $results{$matchLine} = 2 if $results{$matchLine}; #Only when already found in file1
        }
    close(FILE2);  
    open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
    foreach my $matchLine (keys %results) { 
    print OUTPUT $matchLine if $results{$matchLine} ne 1;
    }
    close OUTPUT;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM