By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,244 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,244 IT Pros & Developers. It's quick & easy.

multiple diff script:

P: 51
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,
Feb 7 '08 #1
Share this Question
Share on Google+
12 Replies


KevinADC
Expert 2.5K+
P: 4,059
Lets see what you have written so far to try and accomplish your goal.
Feb 7 '08 #2

P: 51
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
Feb 7 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1. opendir(DIR,'path/to/dir');
  2. my @files = readdir DIR;
  3. close DIR;
For more help you will need to post your current code.
Feb 7 '08 #4

P: 51
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/bin/perl -w 
  4. use File::Compare;
  5.  
  6. my $src_path = "/verification/shaldag/bb_6_1/env";
  7. my $trg_path = "/verification/shaldag/6_1/env";
  8. opendir(SRC,$src_path);
  9. opendir(TRG,$trg_path);
  10.  
  11. my @src_files = readdir SRC; 
  12. my @trg_files = readdir TRG;
  13. my $src_file = ""; 
  14. my $trg_file = ""; 
  15.  
  16. @src_files=sort @src_files;
  17. @trg_files=sort @trg_files;
  18.  
  19. foreach $src_file(@src_files){
  20.   $trg_file = $trg_path.@trg_files($src_file); // this line meant to check if file  is also exist in target directory//
  21.  
  22.   print "$src_file \n"; 
  23.   if (compare($src_file,$src_file) == 0) {
  24.     print "$src_file equal\n";
  25.   }else{
  26.     print "$src_file Not equal\n";
  27.   }
  28. diff_many.pl 
  29.  
  30.  
Feb 11 '08 #5

nithinpes
Expert 100+
P: 410
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/bin/perl -w 
  4. use File::Compare;
  5.  
  6. my $src_path = "/verification/shaldag/bb_6_1/env";
  7. my $trg_path = "/verification/shaldag/6_1/env";
  8. opendir(SRC,$src_path);
  9. opendir(TRG,$trg_path);
  10.  
  11. my @src_files = readdir SRC; 
  12. my @trg_files = readdir TRG;
  13. my $src_file = ""; 
  14. my $trg_file = ""; 
  15.  
  16. @src_files=sort @src_files;
  17. @trg_files=sort @trg_files;
  18.  
  19. foreach $src_file(@src_files){
  20.   $trg_file = $trg_path.@trg_files($src_file); // this line meant to check if file  is also exist in target directory//
  21.  
  22.   print "$src_file \n"; 
  23.   if (compare($src_file,$src_file) == 0) {
  24.     print "$src_file equal\n";
  25.   }else{
  26.     print "$src_file Not equal\n";
  27.   }
  28. diff_many.pl 
  29.  
  30.  
I made slight modification to your script.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2. use File::Compare;
  3.  
  4. my $src_path = "/verification/shaldag/bb_6_1/env";
  5. my $trg_path = "/verification/shaldag/6_1/env";
  6. opendir(SRC,$src_path);
  7. opendir(TRG,$trg_path);
  8.  
  9. my @src_files = readdir SRC; 
  10. my @trg_files = readdir TRG;
  11. my $src_file = ""; 
  12. my $trg_file = ""; 
  13.  
  14. @src_files=sort @src_files;
  15. @trg_files=sort @trg_files;
  16.  
  17. foreach $src_file(@src_files){
  18.   next if($src_file=~/^\.*$/);  ## to skip . and ..
  19.   $exists=grep /^$src_file$/,@trg_files; ## check if file exists in target directory
  20.   unless($exists)  
  21.    { 
  22.   print "$src_file is not present in $trg_path"; 
  23.    next; }
  24.   $trg_file = $trg_path."/$src_file"; # appending the path
  25.   $src_file = $src_path."/$src_file";
  26.   print "$src_file \n"; 
  27.   if (compare($trg_file,$src_file) == 0) {
  28.     print "$src_file and $trg_file equal\n";
  29.   }else{
  30.     print "$src_file and $trg_file Not equal\n";
  31.   }
  32. }
  33.  
This should work
Feb 11 '08 #6

nithinpes
Expert 100+
P: 410
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.

Expand|Select|Wrap|Line Numbers
  1.  $exists=grep /^$src_file$/,@trg_files;
  2.  if ($exists)  {#.......}
  3.  
  4.   OR
  5.  
  6. if(grep /^$src_file$/,@trg_files)
  7. {#........}
  8.  
Feb 11 '08 #7

P: 51
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.

Expand|Select|Wrap|Line Numbers
  1.  $exists=grep /^$src_file$/,@trg_files;
  2.  if ($exists)  {#.......}
  3.  
  4.   OR
  5.  
  6. if(grep /^$src_file$/,@trg_files)
  7. {#........}
  8.  
thanks alot for your help,
i'll try to see how it's work
Feb 11 '08 #8

P: 51
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,
Feb 11 '08 #9

nithinpes
Expert 100+
P: 410
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.
Feb 11 '08 #10

P: 51
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...
Feb 11 '08 #11

nithinpes
Expert 100+
P: 410
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.
Feb 11 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
There is also Text::Diff I have not used it myself but you can look into that module as well.
Feb 11 '08 #13

Post your reply

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