简体   繁体   中英

compare two files from an arbitrary line in Perl

I know that one can compare two files in Perl using:

 use strict; 
 use warnings;
 use File::Compare;

 my $file1 = "example1.txt";
 my $file2 = "Test_2.txt";

 if (compare($file1,$file2) eq 0) 
 {
    print "They're equal\n";
 }else{
    print "They aren't equal\n";
 }

Well, I am interested to know if one can compare files from a desired line. I have huge files and after a fixed line number maybe they are different. It takes very long time to compare the whole files. Then I am looking for a shortest way!

Sure. compare accepts file handles as well as file names, so you can just open the files and read past the lines you want to ignore before you call compare

This example code just ignores the first line from both files. I hope you understand how to skip a given number of lines, or until a regex pattern matches?

use strict;
use warnings;

use File::Compare 'compare';

open my $fh1, '<', 'file1.txt';
open my $fh2, '<', 'file2.txt';

# read past header lines
<$fh1>; 
<$fh2>;

if ( compare( $fh1, $fh2 ) eq 0 ) {
    print "They're equal\n";
}
else {
    print "They aren't equal\n";
}

Let's say the region of interest is after a line that starts with the string BEGIN . You can simply discard all lines from both files until you hit the marker. Doing so, you avoid the cost of the compare function, but you must still pay the IO cost.

$_ = <$fh1> until /^BEGIN/;
$_ = <$fh2> until /^BEGIN/;

If you have some a priori information, for example, the region never begins within the first ten megabytes of the file, you can seek there first.

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