您的位置:首页 > 编程语言

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

2015-05-09 17:26 176 查看
#!/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";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: