去除文件中重复行,保序perl代码。

 

#!/usr/bin/perl

#get scripts name
#print $0 . "\n";

#get the parameters
my $argc = $#ARGV+1;
my $src_file;
my $dst_file;
#print "$argc\n";

if($argc == 2) {
    $src_file = @ARGV[0];
    $dst_file = @ARGV[1];
} elsif ($argc == 1) {
    $src_file = @ARGV[0];
    $dst_file = @ARGV[0]."_tmp";
}
else {
    die "error, <src_file> [dst_file]";
}

my $RD_FILE;
my $WR_FILE;
my $line_str;
my %hash;

open ($RD_FILE ,"<$src_file") || die "Cannot open file $!";
open ($WR_FILE ,">$dst_file") || die "Cannot open file $!";
while($line_str = <$RD_FILE>){
    chomp($line_str);
    if(!($line_str =~ /^\s?$/)){
        if(! $hash{$line_str}){
            #print "$line_str\n";
            print $WR_FILE "$line_str\n";
            $hash{$line_str} = 1;
        }
    }
}
close($RD_FILE);
close($WR_FILE);

print "The $dst_file file has generated.\n";

 

posted @ 2015-05-09 17:26  双手合十  阅读(531)  评论(0编辑  收藏  举报