Hi,
I have one perl script for comparing two text file entries. But its not working according to my expectation. Its comparing line by line, not like comparing one entry to the complete file. I like the script to be compare all the entries irrespective of line by line comparison.

$f1 = 'E:\upload\new\2.txt';
open FILE1, "$f1" or die "Could not open file \n";
$f2= 'E:\upload\new\a.txt';
open FILE2, "$f2" or die "Could not open file \n";

$outfile = 'E:\upload\new\1.txt';

my @outlines;

foreach (<FILE1>) {
$y = 0;
$outer_text = $_;

seek(FILE2,0,0);

foreach (<FILE2>) {
$inner_text = $_;

if($outer_text eq $inner_text) {
$y = 1;
print "$outer_text, Match found \n";
last;
}
}

if($y != 1) {
print "$outer_text,No Match Found \n";
push(@outlines, $outer_text);
}
}

open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
print OUTFILE @outlines;
close OUTFILE;

close FILE1;
close FILE2;

Can anybody please help me to enhace the script.

Thanks in advance
Vinod

Use a hash to compare the lines.

use strict;
use warnings;

my $f1 = 'E:\upload\new\2.txt';
my $f2 = 'E:\upload\new\a.txt';
my $outfile = 'E:\upload\new\1.txt';
my %results = ();

open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;

There are also some perl modules that will compare files, or you can use the operating system commands Salem mentions.

Please use

tags when posting code.
http://www.daniweb.com/techtalkforums/announcement112-3.html

If you're trying to determine which lines are common to both files, then perhaps an easier approach would be to look at the Unix ulilities
- sort
- diff
- comm

Thanks Salem, for the information. But I m try to do the the comparison not like line by line...

Example.

File A------------------------File B
uid=1------------------------uid=4
uid=2------------------------uid=3
uid=3------------------------uid=2
uid=4------------------------uid=1
uid=5
uid=6
In this case you can see file A is having all the values of file be, if we comapare file A with file B its showing "No Match". But i want the comparison like File A should match File B, as the value exist in File B. It will be good if you advice for the same.

Thanks again
Vinod

Didn't like my suggestion?

I think you should be able to pick your answer out of some combination of these commands.

$ head xF*
==> xF1.txt <==
uid=1
uid=2
uid=3
uid=4
uid=5
uid=6

==> xF2.txt <==
uid=4
uid=3
uid=2
uid=1
$ sort -o tmp1.txt xF1.txt && sort -o tmp2.txt xF2.txt
$ diff tmp1.txt tmp2.txt
5,6d4
< uid=5
< uid=6
$ comm -12 tmp1.txt tmp2.txt  # lines common to both files
uid=1
uid=2
uid=3
uid=4
$ comm -13 tmp1.txt tmp2.txt  # lines only in file 2 (none)
$ comm -23 tmp1.txt tmp2.txt  # lines only in file 1
uid=5
uid=6
$ comm -3 tmp1.txt tmp2.txt   # lines unique in file 1 OR file 2 (not  in both)
uid=5
uid=6

Thanks KevinADC, u really saved my lot of time... u r small script did a magic for me.,

Use a hash to compare the lines.

use strict;
use warnings;

my $f1 = 'E:\upload\new\2.txt';
my $f2 = 'E:\upload\new\a.txt';
my $outfile = 'E:\upload\new\1.txt';
my %results = ();

open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;

There are also some perl modules that will compare files, or you can use the operating system commands Salem mentions.

Hello Kevin,
I know this is really a old thread and i should really not be re-opening it. However, the solution you have provided is as mystical as a genie that i couldnt find another better code than the above. I have implemented the above code with certain changes however i require your help to tweak it a further more. Here is a short background,

1. I am trying to compare two text files and find the "missing records" and "mismatch" data in them. The above script gives both missing and mismatch but my requirement is to obtain them individually. What changes would i require to make it work so that it can return me missing data and mismatch data seperately.

Eg : Missing data : File A contains John, denver, marion
File B Contains John, denver

Eg : Mismatch Data: File A Contains John, denver, marion
File B Contains Johnm deeenver, mariion

Thanks much for your help.

Hello Kevin,
1. I am trying to compare two text files and find the "missing records" and "mismatch" data in them. The above script gives both missing and mismatch but my requirement is to obtain them individually. What changes would i require to make it work so that it can return me missing data and mismatch data seperately.

Hello Kevin,
I know this is really a old thread and i should really not be re-opening it. However, the solution you have provided is as mystical as a genie that i couldnt find another better code than the above. I have implemented the above code with certain changes however i require your help to tweak it a further more. Here is a short background,

1. I am trying to compare two text files and find the "missing records" and "mismatch" data in them. The above script gives both missing and mismatch but my requirement is to obtain them individually. What changes would i require to make it work so that it can return me missing data and mismatch data seperately.

Eg : Missing data : File A contains John, denver, marion
File B Contains John, denver

Eg : Mismatch Data: File A Contains John, denver, marion
File B Contains Johnm deeenver, mariion

Thanks much for your help.

Hey have you got the soution. even i need same thing could you help me.

hey guys.... i want to compare two files (line by line) and store same in both files ... my version of coparison

use warnings;
use strict;


# define variables
my $file1 = "./file1";
my $file2 = "./file2";
my $res_file = "./result";

my $f1_linNum = 0;
my $f2_linNum = 0;
my $matched = 0;



open RESULT, ">$res_file" or die "Cannot open $res_file for writing \n";


# store file content into arrays
open F1, "$file1" or die "Could not open file: $! \n";
  my @f1_data=<F1>;   
close(F1);  

open F2, "$file2" or die "Could not open file: $! \n"; 
 my @f2_data=<F2>;
close(F2);

# count lines for each file       
foreach(@f1_data){ ++$f1_linNum; }
foreach(@f2_data){ ++$f2_linNum; }


for(my $x = 0; $x < $f1_linNum; $x++){

      my $f1_line = @f1_data[$x];
      # for every line from file1 look into 
      # file2 and check for matching records
      for(my $y = 0; $y < $f2_linNum; $y++){

        my $f2_line = @f1_data[$y];         
        # if both line matches add it to $matched and 
        # print out into result file 
        if($f1_line == $f2_line){
          $matched += 1; # count match field
          print $f1_line;
        }

      }    


}
close RESULT;


## print final results
print "File1 contains: $f1_linNum lines\n";
print "File2 contains: $bo_linNum lines\n";
print "RESULT: $matched line(s) matched\n"; 

sorry for my english (not origin)

good luck :)

c0d3r

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.