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: Intermediate: Re: [joeferns79] Error reading files from dir: Edit Log



Kenosis
User

Feb 5, 2014, 8:20 PM


Views: 13962
Re: [joeferns79] Error reading files from dir

Looks like you're getting a great handle on this! In case you're interested--and since installing the module's a bit too much, at this time--the following is functionally equivalent to the earlier post, sans File::Find::Rule:

Code
use strict; 
use warnings;
use File::Basename;

my $dir = '/home/tester1/logs';
my ( @paths, %FHs );

for my $dir2 ( grep -d, <$dir/*> ) {
for my $path (<$dir2/*.log>) {
push @paths, $path;
}
}

for my $path (@paths) {
my ( $filename, $suffix ) = ( fileparse($path) )[ 0, 1 ];
my $currDir = basename $suffix;

open my $FILE, '<', $path or die "$path: $!\n";

while (<$FILE>) {
if (/^\Q<string to search>\E/) {
open $FHs{$currDir}, '>', $suffix . "report_$currDir.txt" unless defined $FHs{$currDir};
print { $FHs{$currDir} } $_;
}
}
close $FILE;
}

close $FHs{$_} for keys %FHs;



(This post was edited by Kenosis on Feb 6, 2014, 9:32 AM)


Edit Log:
Post edited by Kenosis (User) on Feb 6, 2014, 8:16 AM
Post edited by Kenosis (User) on Feb 6, 2014, 8:22 AM
Post edited by Kenosis (User) on Feb 6, 2014, 9:26 AM
Post edited by Kenosis (User) on Feb 6, 2014, 9:27 AM
Post edited by Kenosis (User) on Feb 6, 2014, 9:32 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