perl去除重复内容的脚本代码(重复行+数组重复字段)

所属分类: 脚本专栏 / perl 阅读数: 1330
收藏 0 赞 0 分享

假如有这样的一段序列:
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.txt\n"; 
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 $key2\n"; 
       } 
   } 
close OUT;


代码二:

如果有一个文件data有10G大,但是有好多行都是重复的,需要将该文件中重复的行合并为一行,那么我们需要用什么办法来实现
cat data |sort|uniq > new_data #该方法可以实现,但是你需要花上好几个小时。结果才能出来。
下面是一个使用perl脚本来完成此功能的小工具。原理很简单,创建一个hash,每行的内容为键,值由每行出现的次数来填充,脚本如下;

复制代码 代码如下:

#!/usr/bin/perl
# Author :CaoJiangfeng
# Date:2011-09-28
# Version :1.0
use warnings;
use strict;

my %hash;
my $script = $0; # Get the script name

sub 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 rows

open (FILE,"<$source_file") or die "Cannot open file $!\n";
open (SORTED,">$dest_file") or die "Cannot open file $!\n";

while(defined (my $line = <FILE>))
{
        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);

代码三:

通过perl脚本,删除数据组中重复的字段

复制代码 代码如下:

#!/usr/bin/perl
use strict;
my %hash;
my @array = (1..10,5,20,2,3,4,5,5);
#grep 保存符合条件的元素
@array = grep { ++$hash{$_} < 2 } @array;
print join(" ",@array);
print "\n";

更多精彩内容其他人还在看

使用 use re debug 查看正则表达式的匹配过程

使用 use re 'debug' 查看正则表达式的匹配过程,参见如下的代码
收藏 0 赞 0 分享

perl中的$a和$b介绍

有关perl中的$a和$b,这两个变量是为sort函数准备的内置变量,所以声明时可以不加 my
收藏 0 赞 0 分享

perl用{}修饰变量名的写法分享

在perl中用{}修饰变量名,可以防止 _ 被解释为变量名的一部分
收藏 0 赞 0 分享

Perl使用File::Basename获取文件扩展名的代码

本文为大家介绍的这个例子,实现了获取/home/topgkw中所有文件后缀,其中目录返回空值
收藏 0 赞 0 分享

Perl 哈希Hash用法之入门教程

本文和大家重点讨论一下Perl Hash的用法,哈希是一种数据结构,和数组类似,但是,和数组不同的是,其索引不是数字,而是名字。也就是说,索引(这里,我们将它叫key)不是数字而是任意的唯一的字符串
收藏 0 赞 0 分享

perl哈希的一个实例分析

上一篇文章介绍了hash的入门教程,这篇文章为大家提供一个实例,方便大家深入学习
收藏 0 赞 0 分享

Perl哈希表用法解析

Perl语言有很多值得学习的地方,那么你对Perl哈希表的概念是否熟悉呢,这里和大家分享一下,希望本文的介绍能让你有所收获
收藏 0 赞 0 分享

Perl 哈希的创建和引用介绍

创建,引用仅有两种方法,使用它也是两种,这里简单介绍下, 方便需要的朋友
收藏 0 赞 0 分享

Perl 函数集小结

perl中常用的函数集合,特分享下,方便需要的朋友
收藏 0 赞 0 分享

perl的POD权限问题处理

今天我们继续查找mod_perl对req_header的处理,有需要的朋友建议参考学习之
收藏 0 赞 0 分享
查看更多