-1

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

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 occured,how many times BRA.l has occured and so on.

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\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;   
}

Output should look like:

MOV appeared 1 time
RETI appeared 2 times 

Any help is appreciated. Request you to please help on this !!

vk41286
  • 113
  • 1
  • 1
  • 10
  • Yes its not compiling, but if I replace `my @filtered=grep(/^$key$/,@contents);` with `my @filtered=grep(/MOV/,@contents);.` It provides the output. But I want to search MOV, RETI etc instructions. – vk41286 Jan 20 '15 at 06:26
  • You can make regex like `$re = qr/\bMOV\b|\bBRA\.l\b|\bRETI\b|../;` and count words using hash `$seen{$1}++ while $line =~ /($re)/g;` – mpapec Jan 20 '15 at 06:36

3 Answers3

1

I couldn't get your code working, but this code works and is a little easier to read IMO (change the paths back to the ones on your filesystem):

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

The one confusing part is the crazy regex line. I found that on StackOverflow here, and didn't have time to come up with anything cleaner : Is there a Perl shortcut to count the number of matches in a string?

Community
  • 1
  • 1
Mark Madej
  • 1,752
  • 1
  • 14
  • 19
  • Thanks for the code. Much appreciate it. This code works perfectly.You can make it answered. Unfortunately I cant click on answered. – vk41286 Jan 20 '15 at 08:00
0

Check this and try:

#!/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";
    }
}
ssr1012
  • 2,573
  • 1
  • 18
  • 30
  • Hi thanks for the code. But it returns only RETI. I mean it gives only one keyword. Output is `Count :7 Lines RETI RETI` – vk41286 Jan 20 '15 at 07:05
  • print your expected output pls. – ssr1012 Jan 20 '15 at 07:16
  • please find below my output. Also I need output as mentioned in question. Sorry i edited the question. The keyword.txt contains all the assembly insttruction like MOV, ADD, SUB, XOR etc. My input file actually a assembly code of 100 lines. – vk41286 Jan 20 '15 at 07:24
-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
vk41286
  • 113
  • 1
  • 1
  • 10