469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Difference between two files

hi all..

i have just now started working in perl... i have a requirment where in i need to compare two text files.. and need to print the contents that are not present in file 2 during comparison into a third file.. i am able to compare files but dont know how to move the missing contents into new file.. can anyone kindly help me in doing it.. to make things clear all i want to do is:
Expand|Select|Wrap|Line Numbers
  1. sample:
  2. file 1
  3.  
  4. a,"1",g,c
  5.  
  6. file 2
  7.  
  8. a,"1",c
  9.  
  10. after comparison i want  file 3 to hold the missing content that is 
  11.  
  12. file 3
  13.  g
will be thankful to whatever help i get

thanks
mano
Aug 29 '07 #1
13 12786
numberwhun
3,503 Expert Mod 2GB
Well, this sounds like it is part of a class that you are taking. Is that correct? Either way, we can assist, but want to see what you have tried thus far. Please post your code.

I can say this, if you have "Learning Perl, 4th Edition", there is a piece in there that goes over how to compare two files and do exactly what you want.

Also, if you don't have a coy of Learning Perl, then read this blog as it is pretty much the same type of comparison as in the book.

Regards,

Jeff
Aug 29 '07 #2
Can I assume that the file file1 always contain more elements than those of file2 or whether the file2 can contain more elements than those of file1?

Please be clear in that as the code differs in both the cases.
Aug 29 '07 #3
numberwhun
3,503 Expert Mod 2GB
It should not matter if one file has more elements than the other. What the code does is take the first file and put all the unique lines into the output file, disregarding any duplicates. Then, it takes the second file and adds its lines that are unique, again, discarding the duplicates.

Regards,

Jeff
Aug 29 '07 #4
hi all

i just tried comparing the two files with the following code..
Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2.  
  3. if (compare("data(old).txt","data.txt") == 0) {
  4.     print "They're equal\n";
  5. } else {
  6.     open(CHECK,">deletes.txt");
  7.     print "They are not equal";
  8. }
now what happens is it just says they are not equal... since file 1 has more values than file2.. now i want those missing values in file2 to be separetely printed on to file 3. how can i do that. can anyone help me doing it..


thanks to whatever help being offered

thanks

mano
Aug 30 '07 #5
numberwhun
3,503 Expert Mod 2GB
If you read the link that I provided you, then you will see that you will be able to combine your two files into one file without using the File::Compare module. The following Perl one liner is from that link and is also explained there as to how it works:

Expand|Select|Wrap|Line Numbers
  1. perl -e '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"' ./file1 ./file2.txt > ./combined.txt
  2.  
If you have a copy of Learning Perl, this type of comparison is also explained briefly in there as well.

Regards,

Jeff
Aug 30 '07 #6
hi

i tired that lines to compare and copy the contents to newfiles but it shows tan error saying

Expand|Select|Wrap|Line Numbers
  1. bareword found where oprerator expected near the lines
  2.  
  3. "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"' ./file1 ./file2.txt > ./combined.txt
i dont how to proceed can u help in telling what could be done

expecting a reply at the earliest
thanks

mano
Sep 1 '07 #7
numberwhun
3,503 Expert Mod 2GB
Are you executing this as a perl one liner or are you trying to incorporate it into a script? Either way, please post the code as you have entered it.

Regards,

Jeff
Sep 1 '07 #8
hi

i need to put this code inside another script.but this is going to be in separate function so i planned to use it separetly only till now.. the code which i coded is

Expand|Select|Wrap|Line Numbers
  1. '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"'>./data.>/data1.txt > ./combined.txt
where data is the name of file1 and data1 is the second file name. when i executed this script it showed that error which i had mentioned above..

kindly help me

thanks
mano
Sep 2 '07 #9
numberwhun
3,503 Expert Mod 2GB
Ok, I had a feeling that that is what you were trying to do and just wanted to confirm it.

You cannot use the one liners format in your script the way it is. You are going to have to code it into your script, even if it is a separate function.

The reason is, because the way the one liner is, it has single quotes to contain it. Also, you would want to oppen the different input files for reading from and then open the output file for writing to(with a filehandle).

Since you have the necessary code to do the de-duplicadtion, why don't you go ahead and throw some code together to do this, and if you get stuck, post all of the code back here and we will help you. I am not trying to be cruel, but if Ii write it for you, how will you learn? Removing the enclosing single quotes is a good start though, in transforming this code.

Regards,

Jeff
Sep 2 '07 #10
hi jeff

Am sorry i am not able to understand what you are trying to explain.. i have just now started working in perl so dont have much exposure to it..so only am asking.. i too want to learn it myself but just expect some sort of guidence. if u can explain me what sholud be done in a lighter manner will surely try things out myself

thanks

mano
Sep 2 '07 #11
numberwhun
3,503 Expert Mod 2GB
Ok, let me try to explain. The following code:

Expand|Select|Wrap|Line Numbers
  1. '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"'>./data.>/data1.txt > ./combined.txt
  2.  
the way it is shown here, is meant to be executed on the command line, due to it being surrounded by the single quotes = ' and '.

One thing I noticed is that the original code listed the files (ie: file1.txt and file2.txt) one after the other after the statements. What you have done is add a ">" before each file name. This, in shell terms is a redirection OUT to those files. The way it was originally written, it would take each file as input and the last file, preceded by a ">" was what was being outputted to.

I will try and produce something that will take a list of files and process them each into a single file, removing duplicates, but it is going to take a little while as I have some things going on right now.

Regards,

Jeff
Sep 2 '07 #12
hi jeff

i tired removing that redirection symbol that i had inserted then also it showing the same error..hope u can provide me with an example so that i might be able to correlate it with mine....

expecting some kind of help.. sorry for the trouble caused

thanks
mano
Sep 2 '07 #13
numberwhun
3,503 Expert Mod 2GB
The error you are getting about the bare word means that you have a word that is not a recognized command used in your code. If you have the redirection and the files in there, then that is your problem. In perl code, you cannot put a redirect to a file directly. You have to typically print to a file handle. All of the code within the single quotes is valid, but that's about the extent of it. You have to code to use the files in your script.

Like I said, it will take a little while with things going on, but I will try and get you an example.

Regards,

Jeff
Sep 2 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Nimmi Srivastav | last post: by
3 posts views Thread by John Bailo | last post: by
2 posts views Thread by Hai Ly Hoang [MT00KSTN] | last post: by
34 posts views Thread by Mark Sullivan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.