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 $fileBn"; 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 $fileBn"; 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 $fileAn"; if ($countA == 0 and $countB ==0 ){ ? ? print STDOUT "The two files are identicaln"; } close DIFF_B; 测试结果: 输入命令perl diff.pl test1.txt test2.txt test1.txt: slate text2.txt: slate test1.txt.comm.txt: slate test1.txt.diff.txt: my ($fileA,$fileB) = @ARGV; test2.txt.diff.txt: open A,$fileA or die "Unable to open file:$fileA:$!"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |