Compare two Text File

Reply

Join Date: May 2007
Posts: 2
Reputation: srvinu is an unknown quantity at this point 
Solved Threads: 0
srvinu srvinu is offline Offline
Newbie Poster

Compare two Text File

 
0
  #1
May 23rd, 2007
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
Last edited by srvinu; May 23rd, 2007 at 6:11 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Compare two Text File

 
0
  #2
May 23rd, 2007
Please use [code][/code] tags when posting code.
http://www.daniweb.com/techtalkforum...ment112-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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Compare two Text File

 
0
  #3
May 23rd, 2007
Use a hash to compare the lines.

  1. use strict;
  2. use warnings;
  3.  
  4. my $f1 = 'E:\upload\new\2.txt';
  5. my $f2 = 'E:\upload\new\a.txt';
  6. my $outfile = 'E:\upload\new\1.txt';
  7. my %results = ();
  8.  
  9. open FILE1, "$f1" or die "Could not open file: $! \n";
  10. while(my $line = <FILE1>){
  11. $results{$line}=1;
  12. }
  13. close(FILE1);
  14.  
  15. open FILE2, "$f2" or die "Could not open file: $! \n";
  16. while(my $line =<FILE2>) {
  17. $results{$line}++;
  18. }
  19. close(FILE2);
  20.  
  21.  
  22. open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
  23. foreach my $line (keys %results) {
  24. print OUTFILE $line if $results{$line} == 1;
  25. }
  26. close OUTFILE;

There are also some perl modules that will compare files, or you can use the operating system commands Salem mentions.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 2
Reputation: srvinu is an unknown quantity at this point 
Solved Threads: 0
srvinu srvinu is offline Offline
Newbie Poster

Re: Compare two Text File

 
0
  #4
May 24th, 2007
Originally Posted by Salem View Post
Please use tags when posting code.
http://www.daniweb.com/techtalkforum...ment112-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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Compare two Text File

 
0
  #5
May 24th, 2007
Didn't like my suggestion?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Compare two Text File

 
0
  #6
May 24th, 2007
I think you should be able to pick your answer out of some combination of these commands.
  1. $ head xF*
  2. ==> xF1.txt <==
  3. uid=1
  4. uid=2
  5. uid=3
  6. uid=4
  7. uid=5
  8. uid=6
  9.  
  10. ==> xF2.txt <==
  11. uid=4
  12. uid=3
  13. uid=2
  14. uid=1
  15. $ sort -o tmp1.txt xF1.txt && sort -o tmp2.txt xF2.txt
  16. $ diff tmp1.txt tmp2.txt
  17. 5,6d4
  18. < uid=5
  19. < uid=6
  20. $ comm -12 tmp1.txt tmp2.txt # lines common to both files
  21. uid=1
  22. uid=2
  23. uid=3
  24. uid=4
  25. $ comm -13 tmp1.txt tmp2.txt # lines only in file 2 (none)
  26. $ comm -23 tmp1.txt tmp2.txt # lines only in file 1
  27. uid=5
  28. uid=6
  29. $ comm -3 tmp1.txt tmp2.txt # lines unique in file 1 OR file 2 (not in both)
  30. uid=5
  31. uid=6
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC