perl去除重复内容的小脚本,有需要的朋友可以参考下。
假如有这样的一段序列:
1 2  
1 2  
2 1  
1 3  
1 4  
1 5  
4 1 
我们需要得到如下的结果:
1 3  
1 5  
2 1  
4 1 
那么,请借助以下的perl脚本来实现。
 
#!/bin/perl
use strict;  
use warnings;  
      
my $filename;  
my %hash;  
my @information;  
my $key1;  
my $key2;  
      
print "please put in the file like this f:\perl\data.txtn";  
chomp($filename=<STDIN>);  
open(IN,"$filename")||die("can not open");  
while(<IN>)  
{  
   chomp;  
   @information=split/s+/,$_;  
   if(exists $hash{$information[0]}{$information[1]})  
   {  
       next;  
   }  
   else  
   {  
       $hash{$information[0]}{$information[1]}='A';  
    }  
   }  
   close IN;  
     
   open(IN,"$filename")||die("can not open");  
   while(<IN>)  
   {  
       @information=split/s+/,$_;  
       if(exists $hash{$information[1]}{$information[0]})  
       {  
           delete $hash{$information[0]}{$information[1]}  
       }  
       else  
       {  
           next;  
       }  
   }  
   close IN;  
     
   open(OUT,">f:A_B_result.txt")||die("can not open");  
   foreach $key1 (sort{$a<=>$b} keys %hash)  
   {  
       foreach $key2 (sort{$a<=>$b} keys %{$hash{$key1}})  
       {  
           print OUT "$key1 $key2n";  
       }  
   }  
close OUT;