您的位置:首页 > 其它

perl脚本比较两个文件的相同行和不同行

2014-02-13 21:17 393 查看
这个脚本diff.pl用于求两个文件相同的行和不同的行所构成的差集(即A中存在而B中不存在的行,及B中存在而A中不存在的行)。

#!/usr/bin/perl

use 5.010;

use strict;

use warnings;

use diagnostics;#warning info

my ($fileA,$fileB) = @ARGV;

open A,'<',$fileA or die "Unable to open file:$fileA:$!";

my %ta;

my $i;

while(<A>){

chomp;

$ta{$_} = ++$i;

}

close A;

open B,'<',$fileB or die "Unable to open file:$fileB:$!";

#open COMM_AB, ">$fileA.comm" or die "Unable to create comm file for $fileA and $fileB:$!";

open COMM_AB, ">$fileA.comm.txt" or die "Unable to create comm file for $fileA and $fileB:$!";

my $countAB;

my @B;

while(<B>){

chomp;

unless (defined $ta{$_}){

push @B,$_;

}else{

$ta{$_} = 0;

$countAB++;

print COMM_AB $_ ."\n";

}

}

close B;

print "$countAB lines both in $fileA and $fileB\n";

close COMM_AB;

# Output diff to different files respectively

#open DIFF_A, ">$fileA.diff" or die "Unable to create diff file for $fileA:$!";

open DIFF_A, ">$fileA.diff.txt" or die "Unable to create diff file for $fileA:$!";

my $countA;

my %tt = reverse %ta;

foreach (sort keys %tt) {

$countA += $_>0? print DIFF_A $tt{$_}."\n":0;

}

print "$countA lines in $fileA but not in $fileB\n";

close DIFF_A;

#open DIFF_B, ">$fileB.diff" or die "Unable to create diff file for $fileB:$!";

open DIFF_B, ">$fileB.diff.txt" or die "Unable to create diff file for $fileB:$!";

my $countB = scalar @B;

print DIFF_B $_."\n" foreach @B;

print "$countB lines in $fileB but not in $fileA\n";

if ($countA == 0 and $countB ==0 ){

print STDOUT "The two files are identical\n";

}

close DIFF_B;

测试结果: 输入命令perl diff.pl test1.txt test2.txt

test1.txt:

slate

lava

granite

limestone

my ($fileA,$fileC) = @ARGV;

my ($fileA,$fileB) = @ARGV;

while(<A>){

chomp;

$ta{$_} = ++$i;

}

text2.txt:

slate

lava

granite

limestone

open A,'<',$fileA or die "Unable to open file:$fileA:$!";

while(<A>){

chomp;

$ta{$_} = ++$i;

}

test1.txt.comm.txt:

slate

lava

granite

limestone

while(<A>){

chomp;

$ta{$_} = ++$i;

}

test1.txt.diff.txt:

my ($fileA,$fileC) = @ARGV;

my ($fileA,$fileB) = @ARGV;

test2.txt.diff.txt:

open A,'<',$fileA or die "Unable to open file:$fileA:$!";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: