CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [millan] greping a large no keywords in large no of files: Edit Log



Kenosis
User

May 17, 2013, 11:32 AM


Views: 10564
Re: [millan] greping a large no keywords in large no of files

Here's one option:

Code
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:

Code
$VAR1 = [ 
'words.txt',
'print.fmg',
'results.rex'
];

Of course you'll want to remove:

Code
my @onlyFiles  = qw/words.rex print.fmt results.rex/;

and replace

Code
qw/these are my keywords/

with

Code
@keyword

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)


Edit Log:
Post edited by Kenosis (User) on May 17, 2013, 11:33 AM
Post edited by Kenosis (User) on May 17, 2013, 11:48 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives