Fast alternative to grep -f

前端 未结 8 1288
春和景丽
春和景丽 2020-12-11 16:00

file.contain.query.txt

ENST001

ENST002

ENST003

file.to.search.in.txt

ENST001  90

ENST002  80

ENST004  50
8条回答
  •  旧时难觅i
    2020-12-11 16:35

    This Perl code may helps you:

    use strict;
    open my $file1, "<", "file.contain.query.txt" or die $!;
    open my $file2, "<", "file.to.search.in.txt" or die $!;
    
    my %KEYS = ();
    # Hash %KEYS marks the filtered keys by "file.contain.query.txt" file
    
    while(my $line=<$file1>) {
        chomp $line;
        $KEYS{$line} = 1;
    }
    
    while(my $line=<$file2>) {
        if( $line =~ /(\w+)\s+(\d+)/ ) {
            print "$1 $2\n" if $KEYS{$1};
        }
    }
    
    close $file1;
    close $file2;
    

提交回复
热议问题