文章摘要
这篇文章介绍了使用 Perl 语言编写的一个去重脚本。脚本的主要功能包括:通过参数检查确保输入文件的有效性,使用哈希结构统计重复行的次数,并按顺序将统计结果输出到目标文件中。文章详细描述了脚本的结构和功能,突出了其简洁性和高效性。
#!/usr/bin/perl
# Author :CaoJiangfeng
# Date:2011-09-28
# Version :1.0
use warnings;
use strict;my %hash;
my $script=$0; # Get the script namesub usage
{
printf(“Usage:\n”);
printf(“perl $script <source_file> <dest_file>\n”);}
# If the number of parameters less than 2 ,exit the script
if ( $#ARGV+1 < 2) {&usage;
exit 0;
}my $source_file=$ARGV[0]; #File need to remove duplicate rows
my $dest_file=$ARGV[1]; # File after remove duplicates rowsopen (FILE,”<$source_file”) or die “Cannot open file $!\n”;
open (SORTED,”>$dest_file”) or die “Cannot open file $!\n”;while(defined (my $line=
{
chomp($line);
$hash{$line} +=1;
# print “$line,$hash{$line}\n”;
}foreach my $k (keys %hash) {
print SORTED “$k,$hash{$k}\n”;#改行打印出列和该列出现的次数到目标文件
}
close (FILE);
close (SORTED);