
Kenosis
User
May 17, 2013, 11:32 AM
Post #2 of 7
(14914 views)
|
Re: [millan] greping a large no keywords in large no of files
[In reply to]
|
Can't Post
|
|
Here's one option:
use strict; use warnings; use Data::Dumper; my @onlyFiles = qw/words.rex print.fmt results.rex/; my %extensions = ( rex => 'txt', fmt => 'fmg' ); my %keywords = map {lc $_ => 1 } qw/these are my Keywords/; FILE: for my $i ( 0 .. @onlyFiles - 1 ) { open my $fh, '<', $onlyFiles[$i] or die $!; while (<$fh>) { if ( grep $keywords{lc $_}, split ) { close $fh; $onlyFiles[$i] =~ s/.+\.\K([^.]+)$/$extensions{$1} if $extensions{$1}/e; next FILE; } } close $fh; } print Dumper \@onlyFiles; Output from my testing:
$VAR1 = [ 'words.txt', 'print.fmg', 'results.rex' ]; Of course you'll want to remove:
my @onlyFiles = qw/words.rex print.fmt results.rex/; and replace
qw/these are my keywords/ with Additionally, remove use Data::Dumper, as that module was only used for testing purposes. The script creates a hash of your keywords. It then reads all files' lines, splitting each line into "words." The grep checks whether any one of those words are a keyword. (This check is case-insensitive; remove the two lcs if you need it to be case-sensitive.) If a keyword is found, the file is closed, the file extension is changed in the array, and the next file is processed. Hope this helps!
(This post was edited by Kenosis on May 17, 2013, 11:48 AM)
|