简体   繁体   中英

Issue while comparing two files using perl

I have a file soa.csv

/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config,0750
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration,0750
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2.jar,0640

There is another file soa2.csv

/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration,0740
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration/OUI.xml,0740
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1,0750
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2.jar,0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix3.jar,0640

I want to compare these 2 files. For this I am pushing the contents of both the files in 2 separate hashes. such that key is the filePath and value is the file permission after comma in the csv.

My algo is if key of hash1 is present in hash2,compare the value.If different,then print it in a file.

$changedPermissions="FilesWithChangedPermissions.dif";
 unless(open FH3, '>>'.$changedPermissions) {
       die "Unable to create $changedPermissions";
}
foreach my $key2 ( keys %hash2 ) {
    next unless ( exists $hash1{$key2} );
    my $val1 = join(",", sort @{ $hash1{$key2} });
    my $val2 = join(",", sort @{ $hash2{$key2} });
    if ($val1 eq $val2) {
       print FH4 "$key2";
    }
    else {
        print FH3 "$key2 $val1 $val2\n";
    }
}  

Please note I am using join because in future the csv file may contain other attributes like file Size etc. The content of FilesWithChangedPermissions.dif is:

/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2tags.jar 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration/OUI.xml 0740 0740
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix11.war 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2-install.zip 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1 0750 0750
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uixadfrt.jar 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2.jar 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration 0750 0740
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/modules/oracle.uix_11.1.1/uix2.jar 0640 0640
/net/slc06wmg/scratch/aime1/work/IDM/BASEDIR/IDMTOP/products/app/soa/diagnostics/config/registration 0750 0740

Thus,files with same file permissions is also getting printed in the output which is wrong.

Please help..I am new to perl..Any help will be appreciated!!

It's more easy using standard Linux utils: comm and sort content of soa.csv and soa2.csv files before applying.

comm -23 soa.csv soa2.csv – get lines, which appear only in first file.

comm -13 soa.csv soa2.csv - get lines, which appear only in second file.

comm -12 soa.csv soa2.csv - get lines, which appear in both files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM