multiple diff script:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    multiple diff script:

    Hello All,

    i'd like to make a script that gets 2 paths as a parameters and find the matching files between them.
    then it compare between each pair of files with the same name, and outputs error if their contents isn't similar.


    thanks,
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Lets see what you have written so far to try and accomplish your goal.

    Comment

    • zcabeli
      New Member
      • Jan 2008
      • 51

      #3
      i'll try to focus on what hold me to achieving my task:

      1. i don't know how to use the equivalent of ls in perl.
      to save a list of files in the directory within a @list.

      2. what is the equivalent of diff in pel (compare 2 files)

      thats all i think

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        the equivalent of diff is the File::Compare module. See the File::Compare man page. There are a few wqays to get a list of stuff that is in a folder into an array. The most basic way:

        Code:
        opendir(DIR,'path/to/dir');
        my @files = readdir DIR;
        close DIR;
        For more help you will need to post your current code.

        Comment

        • zcabeli
          New Member
          • Jan 2008
          • 51

          #5
          Hi, here's my code... as you can obviously observe from it, i'm pretty new to this all perl stuff. perheps if you could give me some ideas about how to improve it and make it possible to actually compare files from different sources, which requires the ability to check if some element (in this case filename) exists in a list.
          thanks

          Code:
           
          
          #!/usr/bin/perl -w 
          use File::Compare;
          
          my $src_path = "/verification/shaldag/bb_6_1/env";
          my $trg_path = "/verification/shaldag/6_1/env";
          opendir(SRC,$src_path);
          opendir(TRG,$trg_path);
          
          my @src_files = readdir SRC; 
          my @trg_files = readdir TRG;
          my $src_file = ""; 
          my $trg_file = ""; 
          
          @src_files=sort @src_files;
          @trg_files=sort @trg_files;
          
          foreach $src_file(@src_files){
            $trg_file = $trg_path.@trg_files($src_file); // this line meant to check if file  is also exist in target directory//
          
            print "$src_file \n"; 
            if (compare($src_file,$src_file) == 0) {
              print "$src_file equal\n";
            }else{
              print "$src_file Not equal\n";
            }
          diff_many.pl

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by zcabeli
            Hi, here's my code... as you can obviously observe from it, i'm pretty new to this all perl stuff. perheps if you could give me some ideas about how to improve it and make it possible to actually compare files from different sources, which requires the ability to check if some element (in this case filename) exists in a list.
            thanks

            Code:
             
            
            #!/usr/bin/perl -w 
            use File::Compare;
            
            my $src_path = "/verification/shaldag/bb_6_1/env";
            my $trg_path = "/verification/shaldag/6_1/env";
            opendir(SRC,$src_path);
            opendir(TRG,$trg_path);
            
            my @src_files = readdir SRC; 
            my @trg_files = readdir TRG;
            my $src_file = ""; 
            my $trg_file = ""; 
            
            @src_files=sort @src_files;
            @trg_files=sort @trg_files;
            
            foreach $src_file(@src_files){
              $trg_file = $trg_path.@trg_files($src_file); // this line meant to check if file  is also exist in target directory//
            
              print "$src_file \n"; 
              if (compare($src_file,$src_file) == 0) {
                print "$src_file equal\n";
              }else{
                print "$src_file Not equal\n";
              }
            diff_many.pl
            I made slight modification to your script.

            Code:
            #!/usr/bin/perl -w 
            use File::Compare;
             
            my $src_path = "/verification/shaldag/bb_6_1/env";
            my $trg_path = "/verification/shaldag/6_1/env";
            opendir(SRC,$src_path);
            opendir(TRG,$trg_path);
             
            my @src_files = readdir SRC; 
            my @trg_files = readdir TRG;
            my $src_file = ""; 
            my $trg_file = ""; 
             
            @src_files=sort @src_files;
            @trg_files=sort @trg_files;
             
            foreach $src_file(@src_files){
              next if($src_file=~/^\.*$/);  ## to skip . and ..
              $exists=grep /^$src_file$/,@trg_files; ## check if file exists in target directory
              unless($exists)  
               { 
              print "$src_file is not present in $trg_path"; 
               next; }
              $trg_file = $trg_path."/$src_file"; # appending the path
              $src_file = $src_path."/$src_file";
              print "$src_file \n"; 
              if (compare($trg_file,$src_file) == 0) {
                print "$src_file and $trg_file equal\n";
              }else{
                print "$src_file and $trg_file Not equal\n";
              }
            }
            This should work

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              To add further, as in the script above you can use grep function to search if a filename/any element is present in a list.

              [CODE=text]
              $exists=grep /^$src_file$/,@trg_files;
              if ($exists) {#.......}

              OR

              if(grep /^$src_file$/,@trg_files)
              {#........}
              [/CODE]

              Comment

              • zcabeli
                New Member
                • Jan 2008
                • 51

                #8
                Originally posted by nithinpes
                To add further, as in the script above you can use grep function to search if a filename/any element is present in a list.

                [CODE=text]
                $exists=grep /^$src_file$/,@trg_files;
                if ($exists) {#.......}

                OR

                if(grep /^$src_file$/,@trg_files)
                {#........}
                [/CODE]
                thanks alot for your help,
                i'll try to see how it's work

                Comment

                • zcabeli
                  New Member
                  • Jan 2008
                  • 51

                  #9
                  Hi Again,

                  i became more greedy and i wish my script also create a log file with the differentcies of all mismatches between files. can i make compare command to return a string with all mismatches, and not just boolean value ?

                  thanks,

                  Comment

                  • nithinpes
                    Recognized Expert Contributor
                    • Dec 2007
                    • 410

                    #10
                    Originally posted by zcabeli
                    Hi Again,

                    i became more greedy and i wish my script also create a log file with the differentcies of all mismatches between files. can i make compare command to return a string with all mismatches, and not just boolean value ?

                    thanks,
                    You can't use compare() function in File::Compare for this purpose.
                    If the files are different, open file-handles for both and assign them two two arrays.
                    Compare these arrays element by element(line by line) and write into a log file.

                    Comment

                    • zcabeli
                      New Member
                      • Jan 2008
                      • 51

                      #11
                      Originally posted by nithinpes
                      You can't use compare() function in File::Compare for this purpose.
                      If the files are different, open file-handles for both and assign them two two arrays.
                      Compare these arrays element by element(line by line) and write into a log file.

                      Hey,

                      i sought about this idea. however, i'd like to make a "smart" comparison like in cshell command. i guess i just to run diff command from cshell using back ticks...

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        Originally posted by zcabeli
                        Hey,

                        i sought about this idea. however, i'd like to make a "smart" comparison like in cshell command. i guess i just to run diff command from cshell using back ticks...

                        Yes, offcourse. That's better option unless you want to accomplish your task using Perl alone.

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          There is also Text::Diff I have not used it myself but you can look into that module as well.

                          Comment

                          Working...