Comparing Two text files in perl and output the matched result

12,092

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;
Share:
12,092
Maxyie
Author by

Maxyie

Updated on June 04, 2022

Comments

  • Maxyie
    Maxyie almost 2 years

    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