943,634 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 38799
  • Perl RSS
May 23rd, 2007
0

Compare two Text File

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srvinu is offline Offline
2 posts
since May 2007
May 23rd, 2007
0

Re: Compare two Text File

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 23rd, 2007
2

Re: Compare two Text File

Use a hash to compare the lines.

Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
May 24th, 2007
0

Re: Compare two Text File

Click to Expand / Collapse  Quote originally posted by Salem ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srvinu is offline Offline
2 posts
since May 2007
May 24th, 2007
0

Re: Compare two Text File

Didn't like my suggestion?
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
May 24th, 2007
0

Re: Compare two Text File

I think you should be able to pick your answer out of some combination of these commands.
Perl Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 26th, 2010
0
Re: Compare two Text File
Thanks KevinADC, u really saved my lot of time... u r small script did a magic for me.,
Last edited by aniruddha_; Feb 26th, 2010 at 8:44 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aniruddha_ is offline Offline
1 posts
since Feb 2010
Mar 17th, 2010
-1

Require a tweak in the code.

Click to Expand / Collapse  Quote originally posted by KevinADC ...
Use a hash to compare the lines.

Perl Syntax (Toggle Plain Text)
  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.
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
basanth27 is offline Offline
1 posts
since Mar 2010
Mar 24th, 2010
0
Re: Compare two Text File
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gvamohan is offline Offline
2 posts
since Mar 2010
Mar 24th, 2010
-1
Re: Compare two Text File
Click to Expand / Collapse  Quote originally posted by basanth27 ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gvamohan is offline Offline
2 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: can someone give me a hand? captcha integration
Next Thread in Perl Forum Timeline: Removing C comments with Perl, and a twist.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC