Perl:从文件中搜索关键字并显示发生的情况
我有两个文件 1。input.txt 2.关键字.txt input.txt的内容类似于Perl:从文件中搜索关键字并显示发生的情况,perl,perl-module,Perl,Perl Module,我有两个文件 1。input.txt 2.关键字.txt input.txt的内容类似于 .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x68
.src_ref 0 "call.s" 24 first
0x000000 0x5a80 0x0060 BRA.l 0x60
.src_ref 0 "call.s" 30 first
0x000002 0x1bc5 RETI
.src_ref 0 "call.s" 31 first
0x000003 0x6840 MOV R0L,R0L
.src_ref 0 "call.s" 35 first
0x000004 0x1bc5 RETI
keyword.txt包含内容
MOV
BRA.l
RETI
ADD
SUB
..
etc
现在我想阅读这个keyword.txt文件,在input.txt文件中搜索它,找出MOV发生了多少次,BRA.l发生了多少次,依此类推
到目前为止,我已经设法让它从一个文件本身工作。这是密码
#!/usr/bin/perl
use strict;
use warnings;
sub retriver();
my @lines;
my $lines_ref;
my $count;
$lines_ref=retriver();
@lines=@$lines_ref;
$count=@lines;
print "Count :$count\nLines\n";
print join "\n",@lines;
sub retriver()
{
my $file='C:\Users\vk18434\Desktop\input.txt';
my $keyword_file = 'C:\Users\vk18434\Desktop\keywords.txt';
open FILE, $file or die "FILE $file NOT FOUND - $!\n";
my @contents=<FILE>;
open FILE, $keyword_file or die "FILE $file NOT FOUND - $!\n";
my @key=<FILE>;
my @filtered=grep(/^$key$/,@contents);
#my @filtered = grep $_ eq $keywords,@contents;
return \@filtered;
}
感谢您的帮助。请您在这方面给予帮助 检查此项并尝试:
#!/usr/bin/perl
use strict;
use warnings;
my (@text, @lines);
my $lines_ref;
my $count;
$lines_ref = &retriver;
sub retriver
{
my $file='input.txt';
my $keyword_file = 'keywords.txt';
open KEY, $keyword_file or die "FILE $file NOT FOUND - $!\n";
my @key=<KEY>;
my @filtered;
foreach my $keys(@key)
{
my $total = '0';
chomp($keys);
open FILE, $file or die "FILE $file NOT FOUND - $!\n";
while(<FILE>)
{
my $line = $_;
my $counter = () = $line =~ /$keys/gi;
$total = $total + $counter;
}
close(FILE);
print "$keys found in $total\n";
}
}
#/usr/bin/perl
严格使用;
使用警告;
我的(@文本,@行);
我的$lines\u ref;
我的$count;
$lines\u ref=&retriver;
次级检索器
{
我的$file='input.txt';
我的$keyword_文件='keywords.txt';
打开键,$keyword\u file或die“文件$file未找到-$!\n”;
我的@key=;
我的@filter;
foreach my$key(@key)
{
我的$total='0';
咀嚼(钥匙);
打开文件,$FILE或死亡“文件$FILE未找到-$!\n”;
while()
{
我的$line=$\ux;
我的$counter=()=$line=~/$keys/gi;
$total=$total+$counter;
}
关闭(文件);
打印“$total中找到的$keys\n”;
}
}
我无法让您的代码正常工作,但在IMO中,此代码可以正常工作并且更易于阅读(将路径更改回文件系统上的路径):
#/usr/bin/perl
打开(我的$keywordFile,“#perl pe3.pl
Prototype mismatch: sub main::retriver () vs none at pe3.pl line 36.
cygwin warning:
MS-DOS style path detected: C:\Users\xxxxx\Desktop\input.txt
Preferred POSIX equivalent is: /cygdrive/c/Users/xxxxx/Desktop/input.txt
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Count :3
Lines
BRA.l
RETI
RETI
是的,它不是编译,但是如果我用my@filtered=grep(/^$key$/,@contents);
替换my@filtered=grep(/MOV/,@contents);
它提供输出。但是我想搜索MOV,RETI等指令。你可以使用类似正则表达式的$re=qr/\bMOV\b |\bBRA\.l\b | |\bRETI\b | |
并使用hash code>$seen{$1}计算单词++而$line=~/($re)/g、
Hi谢谢你的代码。但是它只返回RETI。我的意思是它只给出一个关键字。输出是Count:7行RETI-RETI
请打印你的预期输出。请在下面找到我的输出。我还需要问题中提到的输出。对不起,我编辑了这个问题。keyword.txt包含所有组装说明,如MOV、ADD、SUB,XOR等。我的输入文件实际上是一个100行的汇编代码。谢谢你的代码。非常感谢。这段代码工作得非常完美。你可以让它得到回答。不幸的是,我不能点击“回答”。
#!/usr/bin/perl
open(my $keywordFile, "<", '/Users/mark/workspace/stackOverflow/keyword.txt')
or die 'Could not open keywords.txt';
foreach my $key(<$keywordFile>) {
chomp $key;
open (my $file, '<', '/Users/mark/workspace/stackOverflow/input.txt')
or die 'Could not open input.txt';
my $count = 0;
foreach my $line (<$file>) {
my $number = () = $line =~ /$key/gi;
$count = $count + $number;
}
close($file);
print "$key was found $count times.\n";
}
Prototype mismatch: sub main::retriver () vs none at pe3.pl line 36.
cygwin warning:
MS-DOS style path detected: C:\Users\xxxxx\Desktop\input.txt
Preferred POSIX equivalent is: /cygdrive/c/Users/xxxxx/Desktop/input.txt
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Count :3
Lines
BRA.l
RETI
RETI