- 论坛徽章:
- 0
|
- use strict;
- use warnings;
- open(File1,'<data1.txt') or die "can not open file:$!\n";my @data1=<File1>;close(File1);
- open(File2,'>data2.txt') or die "can not open file:$!\n";
- my %firstcol=();#第一列的hash
- #先获取第一列的hash的值的计数
- foreach my $line1(@data1)
- {
- if($line1=~m/^(\S+)/)
- {
- $firstcol{$1}++;
- }else
- {
- print File2 "9527你大爷有错误!\n";
- }
- }
- #通过循环,如果计数大于1,则输出
- foreach my $line1(@data1)
- {
- if($line1=~m/^(\S+)/ and $firstcol{$1}>1)
- {
- print File2 $line1;
- }
- }
- close(File1);
- close(File2);
- #先排序文本,然后第一列计数大于壹的就打印出来
- open(File3,'>data3.txt') or die "can not open file:$!\n";
- my @newdata1=sort(@data1);#排序后的data1
- foreach my $line1(@newdata1)
- {
- if($line1=~m/^(\S+)/ and $firstcol{$1}>1)
- {
- print File3 $line1;
- }
- }
- close(File3);
复制代码 这是我写的代码,能得到你的运行结果,还有注释,可能效率不是那么理想,我测试了400w行,十几秒能得到结果,我win8 32bit
|
|