我有一个包含 400000 行的大文件,每行包含许多由制表符分隔的关键字。
而且我还有一个文件,其中包含要匹配的关键字列表。假设此文件充当查找。
因此,对于查找表中的每个关键字,我需要在给定文件中搜索所有出现的关键字。并且应该打印出现的行号。
我试过这个
#!usr/bin/perl
use strict;
use warnings;
my $linenum = 0;
print "Enter the file path of lookup table:";
my $filepath1 = <>;
print "Enter the file path that contains keywords :";
my $filepath2 = <>;
open( FILE1, "< $filepath1" );
open FILE2, "< $filepath2" ;
open OUT, ">", "SampleLineNum.txt";
while( $line = <FILE1> )
{
while( <FILE2> )
{
$linenum = $., last if(/$line/);
}
print OUT "$linenum ";
}
close FILE1;
这给出了关键字的第一次出现。但我需要所有的出现,并且关键字应该完全匹配。
完全匹配面临的问题是,例如我有关键字“hello”和“hello world”
如果我需要匹配“hello”,它会返回包含“hello world”的行号,我的脚本也应该只匹配“hello”并给出它的行号。