Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie - Comparing data in two files

Status
Not open for further replies.

dirtyholmes

Programmer
Mar 17, 2004
43
AU
Hi Guys.

I am very new to perl, and require some assistance in a little problem I have.

I have 2 text files which have the following data

File 1

00242531 HOLLAND 4572357225 7722+09868603.0660966698
11242531 FRANCE 6454353535 7444+06779889.0668896669
54243351 CHINA 7676689998 2451+06988676.0966967977
9842665 DENMARK 8985543327 7851+06898966.3696669669
54242313 HUNGARY 4678777979 8967+06889660.4696798680
44446663 GERMANY 3546456557 8966+06896866.2689666693

File 2

00242531 HOLLAND 4572357225 2222+09868603.0660966698
11242531 FRANCE 6454353535 7444+06779889.0668896669
54243351 CHIN 7676689998 2451+06988676.0966967977
9842665 DENMARK 8985543327 7851+06898966.3696669669
54242313 HUNGARY 4678777979 8967+06889660.4696798680
44446663 GERM NY 3546456557 8966+06896866.2689666693


I want to compare the data from datafile1 to the data from datafile2. As I parse through the files, If the values on corresponding lines from each file are different, I wish to write the two lines with the differences to a new file, so I have a new file containing a the line from data file 1, alongside the same line from data file 2 (containing the difference ).
If the two lines match identically I wish to ignore them and move on.

Can anyone point me in a right direction. ?

 
Hello,

I would suggest something ike the following:

open FILE1, "<file1" or
die "Couldn't open file1\n";
@file1 = <FILE1>;
open FILE2, "<file2" or
die "Couldn't open file2\n";
@file2 = <FILE2>;
open FILE3, ">file3" or
die "Couldn't open file3\n";

foreach $line (@file1) {
if ($line ne @file2[$num]) {
print FILE3 $line."\n".@file2[$num]."\n";
}
$num++
}


Please note this is not tested code, it is just the way I would approach it. I may work, if not its very close.

Hope this helps!!!

Mark G
 
In addition let me explain:

Basically reading each of the two files to compare into an array @file1 and @file2.

The start a loop using the first file

"foreach $line (@file1)"

This says for each entry in the array read the data into $line variable, then use $line to compare with the coresponding array element in @file2 and if they are not equal to each other then output to file3.

Hope this explanation makes sense!!

Mark G
 
Thanks for taking the time to help guys. It will certainly get me up and running.
 
you are describing the *nix diff command

diff file1 file2 >file3

1c1
< 00242531 HOLLAND 4572357225 7722+09868603.0660966698
---
> 00242531 HOLLAND 4572357225 2222+09868603.0660966698
3c3
< 54243351 CHINA 7676689998 2451+06988676.0966967977
---
> 54243351 CHIN 7676689998 2451+06988676.0966967977
6c6
< 44446663 GERMANY 3546456557 8966+06896866.2689666693
---
> 44446663 GERM NY 3546456557 8966+06896866.2689666693
 
arn0ld is right:-

$difference = `diff datafile1.txt datafile2.txt`;
print $difference;


Kind Regards
Duncan
 
On an Windows box you can shell to the fc command instead of diff

HTH
--Paul
 
Code:
#!/usr/bin/perl
open FH1, "<file1.txt" or die "$!";
open FH2, "<file2.txt" or die "$!";
open FH3, ">file3.txt" or die "$!";
while (<FH1>) {
 $line1=$_;
 $line2=<FH2>;
  if ($line1 ne $line2) {
   chomp($line1);
   print FH3 "$line1\n";
   print "$line1\n";
   chomp($line2);
   print FH3 "$line2\n";
   print "$line2\n"
 }
} 
close FH1;
close FH2;
close FH3;
HTH
--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top