开发者

perl + compare numbers (NUM1 and NUM2) between two files

I need to compare chksum (NUM1 and NUM2) between file1 to file2 (see example below down)

  1. The first field in file1 or file2 is the file path
  2. The second field in file1 or file2 is the first chksum
  3. The third field in file1 or file2 is the second chksum

The target is to read from file1 the first field (file path) and to verify if this path exists in file2

If file path exist in file2 then need to compare the chksum numbers between file1 to file2

If chksum equal then need to write the file path + chksum numbers in equal.txt file

else if chksum not equal then need to write the file path + chksum numbers in not_equal.txt file

  • remark (if file path from file1 not exist in file2 then need to write the file path in not_exist.txt file)

I need to do it for all files path in file1 until EOF

Question: Can someone have smart perl script for this?

File1

                                                                     NUM1   NUM2

   /lib/modules/2.6.18-128.el5PAE/kernel/开发者_Python百科drivers/block/cpqarray.ko   1317610  32
   /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/cryptoloop.ko 320619   9
   /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/DAC960.ko     20639107 6
   /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/floppy.ko     9547813  71
   /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/loop.ko       2083034  23
   /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/nbd.ko        6470230  18
   /data/libc-2.5.so                                                 55861    1574
   /bin/libcap.so.1.10                                               03221    12
   /var/libcidn-2.5.so                                               31744    188
   /etc/libcom_err.so.2.1                                            40247    8
   .
   .
   .

File2

                                                                        NUM1    MUM2

     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/cpqarray.ko    541761  232
     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/cryptoloop.ko  224619  9
     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/DAC960.ko      06391   73
     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/floppy.ko      54081   71
     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/loop.ko        08307   23
     /lib/modules/2.6.18-128.el5PAE/kernel/drivers/block/nbd.ko         470275  58
     .
     .
     .
     .
     .


For each file, create a hashtable where the key is the filename and the value is the checksum. Iterate through the filenames from the first file (foreach $file (keys %hash_from_file1)) and check if that filename exists in the hash from the second file. If it does, check that the values of the two hashtables are the same ($hash_from_file1{$file} eq $hash_from_file2{$file}). If those match, then write the file and its hash value to equal.txt. If not, write the file and hash value to not_equal.txt.

Is it possible for there to be an entry in the second file that wouldn't exist in the first file?


mobrule's solution is correct. This is the code:

use strict;
use warnings;

open FIN, "file2";
my $file2_hash = {};
while (<FIN> =~/^(.*?)\s*(\d+)\s*(\d+)$/) {
    $file2_hash->{$1} = "$2_$3";
}
close FIN;

open FIN, "file1";
open EQUAL, ">equal.txt";
open NOT_EQUAL, ">not_equal.txt";
open NOT_EXIST, ">not_exist.txt";

while (<FIN> =~/^(.*?)\s*(\d+)\s*(\d+)$/) {
    my $output_str = "$1\t$2\t$3\n";

    if (not exists $file2_hash->{$1}) {
            print NOT_EXIST $output_str;
    } elsif ($file2_hash->{$1} ne "$2_$3") {
            print NOT_EQUAL $output_str;
    } else {
            print EQUAL $output_str;
    }
}

close FIN;
close EQUAL;
close NOT_EQUAL;
close NOT_EXIST;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜