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

这个脚本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:$!";






  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值