Perl:从文件中搜索关键字并显示事件

时间:2015-01-20 06:18:28

标签: perl perl-module

我有两个文件 的 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 

感谢任何帮助。请求你帮忙!

3 个答案:

答案 0 :(得分:1)

我无法使你的代码正常工作,但是这段代码可以工作,并且更容易阅读IMO(将路径更改回文件系统上的路径):

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

一个令人困惑的部分是疯狂的正则表达式线。我在这里找到StackOverflow,并且没有时间想出更干净的东西:Is there a Perl shortcut to count the number of matches in a string?

答案 1 :(得分:0)

检查并尝试:

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

答案 2 :(得分:-1)

#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