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:$!";
#!/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:$!";
相关文章推荐
- perl比较两个文件,输出不同行
- comm 比较两个文件中相同不同行
- perl比较两个目录中的文件内容是否相同
- 一个用于比较两个文件行中不同的Perl脚本
- 两个独立系统中比较同一名字的文件是否相同
- java使用计算md5校验码方式比较两个文件是否相同
- 已有两个文本文件,今要求编程序从这两个文件中读出各行字符,逐个比较这两个文件中相应的行和列上的字符,如果遇到互不相同的字符,输出它是第几行第几列的字符
- 比较两个文件是否相同,获取文件MD5值, HandlerThread 使用
- java比较两个文件是否相同
- 比较两个文件,并删掉相同的行
- python中比较两个文件是否相同
- perl比较文件数据是否相同(Algorithm::Diff模块)
- 比较两个文件夹内同名文件内容是否相同
- 处理两个文件中的相同行和不同行
- perl 两个字符串输出相同但是比较失败
- 获取文件的哈希值(用来比较两个文件是否相同)
- BASH命令和SHELL脚本总结(11)比较两个文件创建时间的先后
- perl比较两个文件字符串的实例代码
- 【Bash脚本】删除两个目录下相同的文件
- 处理两个文件中的相同行和不同行