今天写了一个脚本,用来将两个文件中,不通的部分保存下来,因为是工作需要,原来的文件是CSV文件,所以用到了Text::CSV_XS这个模块,刚开始学习perl不久,在网上找了一些,也没有我想要的解决方法,所以就自己想了一个代码如下
#!/usr/bin/perl
use Text::CSV_XS; my $csvfile="file1.csv"; my $newfile="file2.csv"; open $old, "<", $csvfile; open $new, "<", $newfile; open DIFF, ">", "diff.txt"; my $csvold = Text::CSV_XS->new ({ binary => 1, eol => $/ }); my $csvnew = Text::CSV_XS->new ({ binary => 1, eol => $/ }); my $count; while(my $rowold=$csvold->getline($old)) { my @item1 = @$rowold; my @item2; my @tt; my $line2; my @t = @item1; my $line = shift @item1; $line = shift @item1; $count=0; while(my $rownew=$csvnew->getline($new)) { @item2 = @$rownew; @tt = @item2; $line2 = shift @item2; $line2 = shift @item2; if($line == $line2) { $count=$count + 1; @t=""; $line=""; # print SAME "$line\n";
# print SAME "@tt\n";
last; } # print SAME "@t\n";
} if($line =~ /^\w+/) { print DIFF "@t\n"; } }
close DIFF;
|
将两个文件不同的地方保存到diff.txt文本文件内
阅读(1302) | 评论(0) | 转发(0) |