简体   繁体   中英

How to compare the data in 2 files in Perl?

For example :

File 1:

Apple
Orange
grapes

File 2:

Orange
grapes
Apple 

I am trying to check whether both files have the same data in different order.

How to do it without using any external module?

This is a simple matter of reading each line of the two files into a hash and then comparing the contents of the two hashes. Basically that is a beginner's programming exercise.

use strict;
use warnings;

# $d{LINE} = TALLY
my ($n, %d) = (1);
while (<>){
    $d{$_} += $n;
    $n *= -1 if eof;
}

# Now get whatever kind of lines you are interested in.
my @same_in_both_files = grep { $d{$_} == 0 } keys %d;
my @surplus_in_file1   = grep { $d{$_} >  0 } keys %d;
my @surplus_in_file2   = grep { $d{$_} <  0 } keys %d;

# Or just get a true-false verdict.
my $files_differ = 1 if grep $_, values %d;

如果要使用perl查找两个文件之间的差异,可以尝试Text :: Diff CPAN模块。

doing it manually is a simple exercise. read the first file into a hashtable of line/linenumber, then delete the second file from that table. if an entery doeant exist, put it into a second table. Anything in the tables indicates stuff that doesn't match, and the tables contain line numbers of the differing lines.

Here is an simple way to do what you want in perl:

In pfile1:

Apple
Orange
grapes

In pfile2:

Orange
grapes
Apple

the perl script:

#!/usr/bin/env perl

open (FILE1, "pfile1") || die ("Can't open file pfile1 for reading");
open (FILE2, "pfile2") || die ("Can't open file pfile2 for reading");

my @file1 = <FILE1>;
my @file2 = <FILE2>;

@sorted_file1 = sort @file1;
@sorted_file2 = sort @file2;

die("Your Files are different\n")
  unless ($#sorted_file1 == $#sorted_file2);

for my $item (0 .. $#sorted_file1) {
  if ($sorted_file1[$item] ne $sorted_file2[$item]) {
    die("Your Files are different\n");
  }
}
print "Your Files are the same\n";

This works by reading the files lines into an array, then sorting the array. It checks that both arrays are the same length, then exits early if the corresponding index value between the two arrays is different.

You will then get a message stating that the files are the same...or not.

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