-4

I have an array of files. Now I need to cat each file and search for a list of keywords which is in file keywords.txt.

my keywords.txt contains below

AES
3DES
MD5
DES
SHA-1
SHA-256
SHA-512
10.*
http://
www.
@john.com
john.com

and I'm expecting output as below

file jack.txt contains AES:5 (5 line number) http://:55
file new.txt contains 3DES:75 http://:105
Borodin
  • 126,100
  • 9
  • 70
  • 144

3 Answers3

0

Okay Here is my Code

use warnings;
 use strict;

 open STDOUT, '>>', "my_stdout_file.txt";


 my $filename = $ARGV[2];
  chomp ($filename);
  open my $fh, q[<], shift or die $!;

   my %keyword = map { chomp; $_ => 1 } <$fh>;
   print "$fh\n";
   while ( <> ) {
    chomp;
    my @words = split;
    for ( my $i = 0; $i <= $#words; $i++ ) {
        if ( $keyword{ $words[ $i ] } ) {
                print "Keyword Found for file:$filename\n";
                printf qq[$filename Line: %4d\tWord position:         %4d\tKeyword: %s\n],
                        $., $i, $words[ $i ];
        }
}
}

But the problem is its considering all the Arguments and trying to open the files for ARGV[2]. Actually i need to Open only ARGV[0] and ARGV[1]. ARGV[2] i kept for writing in output only. Appreciate responses. Thanks.

0

I think maybe there are multipe keywords in one line of those txt files. So below code for your reference:

my @keywords;
open IN, "keywords.txt";
while(<IN>) {
    if(/^(.+)$/) {
        push(@keywords, $1); 
    }
} 
close(IN);   

my @filelist=glob("*.txt");
foreach my $filename(@filelist) {
    if(open my $fh, '<', $filename) {
        my $ln=1;
        while(my $line = <$fh>) {
            foreach my $kw(@keywords) {
                if($line=~/$kw/) {
                     print $filename.':'.$ln.':'.$kw."\n";
                }
            }
            $ln++;
        }
    }
    close($fh);
}
laojianke
  • 44
  • 4
  • i'm sorry this code returns error ! `Global symbol "$i" requires explicit package name. Global symbol "$fh" requires explicit package name`. – Sel Vin Jul 19 '16 at 17:17
0

Ok but one thing i noticed here is..

the Keywords

http://
oracle.com

it is matching the whole word, instead it should try to match as part of strings.. Only these keywords.. Others should be searched as per the above.