CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file

 



Rancho
New User

Apr 7, 2013, 8:07 PM

Post #1 of 4 (373 views)
Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file Can't Post

I have a code which does the following.

DirA and DirB have files f1 ,f2 ,f3 ,f4, f5 (not necessarily 5 in number). I need to compare the contents of f1 in DirA with contents of f1 in DirB and output the result in f1_diff. Similarly need to do this for all files in directories A and B. So in the above case assuming the contents of all files are different, Script will output 5 files f1_diff,f2_diff,f3_diff,f4_diff,f5_diff which has the missing lines of both f1 DirA and f1 DirB.

For example, if

f1 dir A has line1, line2, line3, line4xxxx, line5
f1 dir B has line1, line2xxxx, line3, line4, line5
f1_diff should have

line2 --> line2xxxx

line4xxxx -->line4

I need to replace "use Text::Diff " with some code so that it does the same function. I am not supposed to use that perl utility Frown. Can some one help me remodify the below code by replacing text::diff with an alternate piece of code

#!/usr/bin/perl

use Text::Diff;
use strict;
use warnings;

opendir my($dh), "DirA" or die "Couldn't open dir DirA!";
my @files = readdir $dh;
closedir $dh;

foreach my $file (@files) {
next if -d $file;
my $diff = diff "DirA/$file", "DirB/$file";
open DIFF_FILE, ">$file"."_diff";
print DIFF_FILE $diff;
close DIFF_FILE;
}


g4143
Novice

Apr 8, 2013, 3:05 AM

Post #2 of 4 (355 views)
Re: [Rancho] Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file [In reply to] Can't Post

Won't it make sense to filter your the reply of readdir with grep?


Code
#!/usr/bin/perl 

use warnings;
use strict;

print "Enter dir->";
chomp(my $dir = <STDIN>);

opendir(my $DIR1, $dir) or die ("Could not open directory handle! $!\n");

my @dir_entries = sort grep { -r -f } readdir $DIR1;

foreach ( @dir_entries )
{
print $_, "\n";
}

__END__



(This post was edited by g4143 on Apr 8, 2013, 3:07 AM)


Laurent_R
Veteran / Moderator

Apr 8, 2013, 4:17 AM

Post #3 of 4 (348 views)
Re: [Rancho] Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file [In reply to] Can't Post

You might want to take a look at the glob function. It is usually the easiest way to get a list of files matching a pattern.


BillKSmith
Veteran

Apr 8, 2013, 8:18 PM

Post #4 of 4 (323 views)
Re: [Rancho] Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file [In reply to] Can't Post

You want to do the same thing as Text::Diff. Read its documentation carefully. It suggests that there are times that it is better to shell out to a system executable rather than use the module. That would meet the requirement as you state it! The same document states that Text::Diff uses Algorithm::Diff. That is also a possibility for you.

Developing the entire application in native perl would be a major project. I suspect that it is far beyond the help that you can expect here. I would recommend studying both the documentation and code for Algorithm::Diff to learn the algorithm.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives