0

I have two files

  1. input.txt
  2. keyword.txt

input.txt has contents like:

.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 has contents like:

MOV
BRA.l
RETI
ADD
SUB
..
etc

Now I want to read this keyword.txt file and search it in input.txt file and find how many times MOV has occurred how many times BRA.l has occurred.

So far I have managed to get it working from a single file itself. here is the code

#!/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\vk41286\Desktop\input.txt';
    open FILE, $file or die "FILE $file NOT FOUND - $!\n";
    my @contents=<FILE>;

    my @filtered=grep(/MOV R0L,R0L/,@contents);
    return \@filtered;
}

Here I can search only MOV and I am unable to search other instructions like RETI.

Also I want to put MOV,RETI etc. in a keyword.txt file and make it generic.

OUTPUT should be:

MOV has occured 2  times
RETI has occured 1 time

2 Answers 2

1

If you are not hard pressed for perl, a simple command line

 grep -f keyword.txt -c input.txt

should do it.

In perl, you would need to do also open keyword.txt and loop through each keyword, grepping in turn as you have done for 1 alone in your code.

0

It seems that bash-script is much simple than perl:

while read keyword
do
    occurrence =$(grep -c -F "$keyword" input.txt)
    echo "$keyword has occurred $occurrence time(s)"
done < keyword.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .