【发布时间】:2012-01-30 13:34:18
【问题描述】:
我有 2 个大文件(制表符分隔)。
第一个文件 ->
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 101_#2 1 2 F0 263 278 2 1.5 102_#1 1 6 F1 766 781 1 1.0 103_#1 2 15 V1 526 581 1 0.0 103_#1 2 9 V2 124 134 1 1.3 104_#1 1 12 V3 137 172 1 1.0 105_#1 1 17 F2 766 771 1 1.0
第二个文件->
Col1 Col2 Col3 Col4 97486 9 262 279 67486 9 118 119 87486 9 183 185 248233 9 124 134
我想将文件 1 的 col5 和 col6(如范围值)与文件 2 的 col3 和 col4 进行比较。如果文件 1 的范围存在于文件 2 中,则返回该行(从文件 1)。
预期输出 ->
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 101_#2 1 2 F0 263 278 2 1.5 103_#1 2 9 V2 124 134 1 1.3
到目前为止我已经尝试过->
@ARGV or die "No input file specified";
open my $first, '<',$ARGV[0] or die "Unable to open input file: $!";
open my $second,'<', $ARGV[1] or die "Unable to open input file: $!";
print scalar (<$first>);
while (<$first>) {
@cols = split /\s+/;
$p1 = $cols[4];
$p2 = $cols[5];
while(<$second>) {
@sec=split /\s+/;
print join("\t",@cols),"\n" if ($p1>=$sec[2] && $p2<=$sec[3]);
}
}
但这仅适用于第一行。文件也很大(大约 6GB)。
我刚刚尝试了一些带有哈希的东西。
@ARGV or die "No input file specified";
open my $first, '<',$ARGV[0] or die "Unable to open input file: $!";
open my $second,'<', $ARGV[1] or die "Unable to open input file: $!";
print scalar (<$first>);
while(<$second>){
chomp;
@line=split /\s+/;
$hash{$line[2]}=$line[3];
}
while (<$first>) {
@cols = split /\s+/;
$p1 = $cols[4];
$p2 = $cols[5];
foreach $key (sort keys %hash){
if ($p1>= "$key"){
if ($p2<=$hash{$key})
{
print join("\t",@cols),"\n";
}
}
else{next;}
}
}
但这也需要大量的时间和内存。任何人都可以建议我如何使用哈希使其快速。非常感谢。
【问题讨论】:
-
包含这些范围的第一个文件也是千兆字节大小?在这种情况下,您可能希望将这些数据(其中一个文件就足够了)放入某个数据库并查询它以获得结果。
-
我说的是总尺寸。我的第一个文件大约 3 GB,第二个文件大约 1 GB。
标签: perl