Link to home
Start Free TrialLog in
Avatar of richsark
richsarkFlag for United States of America

asked on

need a 3rd path on my check script

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

Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

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>;

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

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

ASKER

Hello,

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

https://www.experts-exchange.com/questions/24589634/need-a-3rd-path-on-my-check-script.html

so mrjoltcola, could you take a look at the print statement on line 47 to 49, is that right?
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.
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

what do you want printed from the results?
 
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
Well??
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

Hello,

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

Thank you in advance
Avatar of Todd Mummert
Todd Mummert


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...
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:
https://www.experts-exchange.com/questions/24581617/Need-to-isloate-differences-matched-in-csv-files-using-perl.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

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.




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
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
ASKER CERTIFIED SOLUTION
Avatar of Todd Mummert
Todd Mummert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
climbgunks is on the way to become a Genius !