Difference between two files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manoprasanna
    New Member
    • Aug 2007
    • 10

    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:
    Code:
    sample:
    file 1
     
    a,"1",g,c
    
    file 2
    
    a,"1",c
    
    after comparison i want  file 3 to hold the missing content that is 
    
    file 3
     g
    will be thankful to whatever help i get

    thanks
    mano
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • rellaboyina
      New Member
      • Jan 2007
      • 55

      #3
      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.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • manoprasanna
          New Member
          • Aug 2007
          • 10

          #5
          hi all

          i just tried comparing the two files with the following code..
          [CODE=perl]use File::Compare;

          if (compare("data( old).txt","data .txt") == 0) {
          print "They're equal\n";
          } else {
          open(CHECK,">de letes.txt");
          print "They are not equal";
          }[/CODE]
          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

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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:

            [code=perl]
            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
            [/code]

            If you have a copy of Learning Perl, this type of comparison is also explained briefly in there as well.

            Regards,

            Jeff

            Comment

            • manoprasanna
              New Member
              • Aug 2007
              • 10

              #7
              hi

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

              Code:
              bareword found where oprerator expected near the lines
              
              "\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

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                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

                Comment

                • manoprasanna
                  New Member
                  • Aug 2007
                  • 10

                  #9
                  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

                  Code:
                  '$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

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    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

                    Comment

                    • manoprasanna
                      New Member
                      • Aug 2007
                      • 10

                      #11
                      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

                      Comment

                      • numberwhun
                        Recognized Expert Moderator Specialist
                        • May 2007
                        • 3467

                        #12
                        Ok, let me try to explain. The following code:

                        [code=perl]
                        '$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
                        [/code]

                        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

                        Comment

                        • manoprasanna
                          New Member
                          • Aug 2007
                          • 10

                          #13
                          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

                          Comment

                          • numberwhun
                            Recognized Expert Moderator Specialist
                            • May 2007
                            • 3467

                            #14
                            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

                            Comment

                            Working...