【问题标题】:Issue while comparing two files using perl使用 perl 比较两个文件时出现问题
【发布时间】:2014-04-18 06:35:01
【问题描述】:

我有一个文件 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

还有另一个文件 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

我想比较这两个文件。 为此,我将两个文件的内容推送到 2 个单独的哈希中。 这样key是filePath,value是csv中逗号后的文件权限。

我的算法是如果 hash1 的键存在于 hash2 中,比较值。如果不同,则将其打印到文件中。

$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";
    }
}  

请注意我正在使用连接,因为将来 csv 文件可能包含其他属性,如文件大小等。 FilesWithChangedPermissions.dif 的内容是:

/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

因此,具有相同文件权限的文件也会打印在输出中,这是错误的。

请帮助..我是 perl 新手..任何帮助将不胜感激!

【问题讨论】:

  • 请包含一个完整的程序,可用于在您的问题中重现您的问题。

标签: perl


【解决方案1】:

使用标准 Linux utils 更容易:应用前 soa.csv 和 soa2.csv 文件的 comm 和 sort 内容。

comm -23 soa.csv soa2.csv – 获取仅出现在第一个文件中的行。

comm -13 soa.csv soa2.csv - 获取仅出现在第二个文件中的行。

comm -12 soa.csv soa2.csv - 获取出现在两个文件中的行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 2012-03-18
    • 2014-06-14
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    相关资源
    最近更新 更多