need a 3rd path on my check script

richsark
richsark used Ask the Experts™
on
Hello,

I have tried to add a 3rd dir to compare, but it does not run, I think I messed this up.

Could I ask an expert to repair so that I can have a 3rd directory to check against.

Keep in mind that I am trying to compare with whats on the $bpath AKA k:\merged

Thanks
#!/usr/bin/perl -w
use strict;
 
my $apath = "k:/raw";
my $bpath = "k:/merged";
my $cpath = "k:/raw2";
my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %b = map {s,^$cpath/,,; $_ => 1} <$bpath/*.csv>;
 
foreach (keys %a) {
    unless (exists $b{$_}) {
        print "$_ not found in $bpath\n";
        delete $a{$_};
    }
}
foreach (keys %b) {
    print "$_ not found in $apath\n" unless exists $a{$_};
}
 
my @fields = ('SubnetAddress',
              'SubnetName',
              'SubnetMask',
              'NetworkAddress',
              'Domain',
              'TftpServer',
              'DNSServers',
              'TimeServers',
              'DefaultRouters',
              'DHCPServer',
              'DHCPOptionTemplate',
              'DHCPPolicyTemplate',
              'AllowDHCPClientsModifyDynamicObjectResourceRecords');
 
foreach my $file (keys %a) {
    my $af = readFile("$apath/$file");
    my $bf = readFile("$bpath/$file");
    my $cf = readFile("$cpath/$file");
    my $diffFound = 0;
 
    foreach my $field (@fields) {
        if ($af->{$field} ne $bf->{$field}) {
            if ($diffFound == 0) {
                print "\nFile $file has the following differences:\n";
                $diffFound = 1;
            }
            print "\t< $field=$af->{$field}\n";
            print "\t> $field=$bf->{$field}\n";
            print "\t> $field=$cf->{$field}\n";
        }
    }
}
 
exit;
 
sub readFile {
    my $file = shift;
    my %rval;
    open (FILE, "<$file") or die "Could not open $file\n";
    while (<FILE>) {
        next unless /^\s*([^=]+)=(.*?)\s*$/;
        $rval{$1} = $2;
    }
    close FILE;
    return { %rval };
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
First, your requirements are too vague. "compare with" and "check against" don't give anyone enough info to write code.

But... you have an error.

Instead of:

my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %b = map {s,^$cpath/,,; $_ => 1} <$bpath/*.csv>;

You probably want:

my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %c = map {s,^$cpath/,,; $_ => 1} <$cpath/*.csv>;

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Unless you mean
my %b = ((map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>),
               (map {s,^$cpath/,,; $_ => 1} <$bpath/*.csv>));

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Unless you mean
my %b = ((map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>),
               (map {s,^$cpath/,,; $_ => 1} <$cpath/*.csv>));
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
Hello,

Sorry about being to vague, more info is found here:

http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24589634.html

so mrjoltcola, could you take a look at the print statement on line 47 to 49, is that right?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
the print statements on line 47 to 49, are valid perl statements
whether they print what you want  to print, only you can tell us.

Author

Commented:
HI Ozo, I know there valid perl commands, but according to what I want printed from the results that the script obtains, will it print accordingly?

I am asking if you can take a look a the the whole script to see how it flows plz

( modified based on the comments above)

#!/usr/bin/perl -w
use strict;
 
my $apath = "k:/raw";
my $bpath = "k:/merged";
my $cpath = "k:/raw2";
my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %c = map {s,^$cpath/,,; $_ => 1} <$cpath/*.csv>;
 
foreach (keys %a) {
    unless (exists $b{$_}) {
        print "$_ not found in $bpath\n";
        delete $a{$_};
    }
}
foreach (keys %b) {
    print "$_ not found in $apath\n" unless exists $a{$_};
}
 
my @fields = ('SubnetAddress',
              'SubnetName',
              'SubnetMask',
              'NetworkAddress',
              'Domain',
              'TftpServer',
              'DNSServers',
              'TimeServers',
              'DefaultRouters',
              'DHCPServer',
              'DHCPOptionTemplate',
              'DHCPPolicyTemplate',
              'AllowDHCPClientsModifyDynamicObjectResourceRecords');
 
foreach my $file (keys %a) {
    my $af = readFile("$apath/$file");
    my $bf = readFile("$bpath/$file");
    my $cf = readFile("$cpath/$file");
    my $diffFound = 0;
 
    foreach my $field (@fields) {
        if ($af->{$field} ne $bf->{$field}) {
            if ($diffFound == 0) {
                print "\nFile $file has the following differences:\n";
                $diffFound = 1;
            }
            print "\t< $field=$af->{$field}\n";
            print "\t> $field=$bf->{$field}\n";
            print "\t> $field=$cf->{$field}\n";
        }
    }
}
 
exit;
 
sub readFile {
    my $file = shift;
    my %rval;
    open (FILE, "<$file") or die "Could not open $file\n";
    while (<FILE>) {
        next unless /^\s*([^=]+)=(.*?)\s*$/;
        $rval{$1} = $2;
    }
    close FILE;
    return { %rval };
}

Open in new window

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
what do you want printed from the results?
 

Author

Commented:
Well, I was hoping to add raw2 to this difference

File 90.215.242.262.csv has the following differences:
      raw      SubnetName=
      merged      SubnetName=mig-
      raw      AllowDHCPClientsModifyDynamicObjectResourceRecords=Same As in Global
      merged      AllowDHCPClientsModifyDynamicObjectResourceRecords=False

Author

Commented:
Well??

Author

Commented:
Ok. I am still getting an error

Could not open d:/raw2/176.78.0.0.csv

Can you please review the script below and correct.

Thanks

#!perl -w
use strict;
 
my $apath = "d:/raw";
my $bpath = "d:/merged";
my $cpath = "d:/raw2";
my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %c = map {s,^$cpath/,,; $_ => 1} <$cpath/*.csv>;
 
foreach (keys %a) {
    unless (exists $b{$_}) {
        print "$_ not found in $bpath\n";
        delete $a{$_};
    
foreach (keys %c) {
    unless (exists $b{$_}) {
        print "$_ not found in $bpath\n";
        delete $a{$_};
    }}
}
foreach (keys %b) {
    print "$_ not found in $apath\n" unless exists $a{$_};
}
 
my @fields = ('SubnetAddress',
              'SubnetName',
              'SubnetMask',
              'NetworkAddress',
              'Domain',
              'TftpServer',
              'DNSServers',
              'TimeServers',
              'DefaultRouters',
              'DHCPServer',
              'DHCPOptionTemplate',
              'DHCPPolicyTemplate',
              'AllowDHCPClientsModifyDynamicObjectResourceRecords');
 
foreach my $file (keys %a) {
    my $af = readFile("$apath/$file");
    my $bf = readFile("$bpath/$file");
    my $cf = readFile("$cpath/$file");
    my $diffFound = 0;
 
    foreach my $field (@fields) {
        if ($af->{$field} ne $bf->{$field}) {
            if ($diffFound == 0) {
                print "\nFile $file has the following differences:\n";
                $diffFound = 1;
            }
            print "\t< $field=$af->{$field}\n";
            print "\t> $field=$bf->{$field}\n";
            print "\t> $field=$cf->{$field}\n";
        }
    }
}
}
 
exit;
 
sub readFile {
    my $file = shift;
    my %rval;
    open (FILE, "<$file") or die "Could not open $file\n";
    while (<FILE>) {
        next unless /^\s*([^=]+)=(.*?)\s*$/;
        $rval{$1} = $2;
    }
    close FILE;
    return { %rval };
}

Open in new window

Author

Commented:
Hello,

Is there any calcification needed to get some answers? I would really like some help please.

Thank you in advance

I wrote the first script, but I don't understand what the significance of the 3rd directory is?


for example,
  if a file (call it X) is only found in A and B (and not in C).... what do you want to have happen?   diff A and B, and report on the missing C?

  if the file exists in A, B, and C, but the contents differ in all three, how do you want it reported.   If the contents only differ in one of the three files...  

 But the code you added doesn't deal with any of these issues...

Author

Commented:
Hi climbgunks:

The issue is this, I have 2 systems that will be merged into one, so systems A and B are totally two different systems. The goal is I am try to achieve is to mesh these two systems into 1 system.

So.... originally we had a compare between system A called "raw" to the system that is merged already named "merged"

All we are doing to the equation is adding the 2nd system called "raw2" to compare with "merged"

A=raw
B=raw 2

C=merged

Take the raw csv files form A and B and compare whats in C and report just like you did originally on top. All I am asking is to add another directory to comparison

I think the answer is this to your question on the above thread:

"if a file (call it X) is only found in A and B (and not in C).... what do you want to have happen?   diff A and B, and report on the missing C?"

Does that help?.
 


Let's take the easy approach and see where the following fails to do what you need:

Here's the original script modified to take 2 directories from the command line.   So if you call this script in this order:

perl script.pl  k:/raw1 k:/merged
perl script.pl  k:/raw2 k:/merged
perl script.pl  k:/raw1 k:/raw2

what is missing that you'd like to see.

For the others, this is the original question:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24581617.html
#!/usr/bin/perl -w
use strict;
 
die "perl $0 dir1 dir2\n" unless @ARGV == 2;
 
my ($apath, $bpath) = @ARGV;
my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
 
foreach (keys %a) {
    unless (exists $b{$_}) {
        print "$_ not found in $bpath\n";
        delete $a{$_};
    }
}
foreach (keys %b) {
    print "$_ not found in $apath\n" unless exists $a{$_};
}
 
my @fields = ('SubnetAddress',
              'SubnetName',
              'SubnetMask',
              'NetworkAddress',
              'Domain',
              'TftpServer',
              'DNSServers',
              'TimeServers',
              'DefaultRouters',
              'DHCPServer',
              'DHCPOptionTemplate',
              'DHCPPolicyTemplate',
              'AllowDHCPClientsModifyDynamicObjectResourceRecords');
 
foreach my $file (keys %a) {
    my $af = readFile("$apath/$file");
    my $bf = readFile("$bpath/$file");
    my $diffFound = 0;
 
    foreach my $field (@fields) {
        if ($af->{$field} ne $bf->{$field}) {
            if ($diffFound == 0) {
                print "\nFile $file has the following differences:\n";
                $diffFound = 1;
            }
            
            print "\t$apath\t$field=$af->{$field}\n";
            print "\t$bpath\t$field=$bf->{$field}\n";
        }
    }
}
 
exit;
 
sub readFile {
    my $file = shift;
    my %rval;
    open (FILE, "<$file") or die "Could not open $file\n";
    while (<FILE>) {
        next unless /^\s*([^=]+)=(.*?)\s*$/;
        $rval{$1} = $2;
    }
    close FILE;
    return { %rval };
}

Open in new window

Author

Commented:
So where are the paths defined for raw, raw2 and merged? I dont see it above?

the orginal has these defined

my $apath = "d:/raw";
my $bpath = "d:/merged";
my $cpath = "d:/raw2";
my %a = map {s,^$apath/,,; $_ => 1} <$apath/*.csv>;
my %b = map {s,^$bpath/,,; $_ => 1} <$bpath/*.csv>;
my %c = map {s,^$cpath/,,; $_ => 1} <$cpath/*.csv>;So where are the paths defined for raw, raw2 and merged? I dont see it above?

Thanks

from my note above.

****      Here's the original script modified to take 2 directories from the command line.   ****

So if you call this script in this order:

perl script.pl  d:/raw d:/merged
perl script.pl  d:/raw2 d:/merged
perl script.pl  d:/raw d:/raw2

I'm suggesting running the script against the three directories in a pairwise ordering...  Otherwise, this is going to devolve into guessing what you want outputted in a 3-way diff...of which there are many possibilities.




Author

Commented:
OK, so the completed code is above?

 all I want to see is how raw, raw2 differ from merged exactly what you did before with the report.

If  the completed code is not there, can you please post it on how it should look like

Thanks

Author

Commented:
Hello, Did I miss something above? or am I asking the wrong question since I did not get a conformation on the code.

Please except my apology if I dont get it

Thanks

If you had tried the code, you would have seen that it ran, or if it didn't run in your environment, you could have posted the error it generated.   That would have been faster than me saying I thought the code was correct and you then trying it.

Simply put,  no one here really knows what type of output you want from a 3-way diff, or in fact, if you actually want a 3-way diff, or just a 2-way diff with different inputs.

I modified the original code to do a 2-way diff which takes the paths from the command line...  therefore you can compare raw with merged, raw2 with merged, and raw against raw2 if you want.    This may be all that you need, but if not, we can then work from there to see what you really want....   rather than making a guess that is almost certain to be wrong.  

Author

Commented:
Hello climbgunks:

All is good with your script. I am happy

Sorry for the delay... I had a death in the family....

I appreciate all your help

Author

Commented:
climbgunks is on the way to become a Genius !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial