Perl:从文件中搜索关键字并显示出现的次数

Perl : search a keywords from a file and display the occurence

我有两个文件 1。 input.txt 2. keyword.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    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;   
}

输出应如下所示:

MOV appeared 1 time
RETI appeared 2 times 

感谢任何帮助。请求您对此提供帮助!

检查并尝试:

#!/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";
    }
}

我无法使您的代码正常工作,但此代码有效并且更易于阅读 IMO(将路径更改回您的文件系统中的路径):

#!/usr/bin/perl

open(my $keywordFile, "<", '/Users/mark/workspace/Whosebug/keyword.txt')
         or die 'Could not open keywords.txt';

foreach my $key(<$keywordFile>) {
        chomp $key;
        open (my $file, '<', '/Users/mark/workspace/Whosebug/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";
}

令人困惑的部分是疯狂的正则表达式行。我在这里的 Whosebug 上发现了这一点,但没有时间想出任何更干净的东西:Is there a Perl shortcut to count the number of matches in a string?

# 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