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



joeferns79
Novice

Jan 21, 2014, 12:31 PM


Views: 24658
Re: [Laurent_R] Error reading files from dir

Thanks, Fishmonger/Laurent_R!

So, I wanted to take this a little further and read multiple directories from the parent dir, and read multiple files from within each dir. In addition, I wanted to output a file for each of these directories based on a search pattern.

This is what I have so far but it doesn't seem to output anything except it prints the directory names...

Code
#!/usr/bin/perl  

use strict;
use warnings;


my $dir = '/home/tester1/logs/';

opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {

next unless (-d "$dir/$file");
print "$file\n";
chdir '$dir/$file'; #go into dirX inside of /home/tester1/logs/
opendir my $DIR2, '.' or die "opendir .: $!\n";
my @files2 = grep /\.log$/i, readdir $DIR2;

open my $out,'>',"../report_$file.txt";

foreach my $file1 (@files2) {
open my $FILE, '<', $file1 or die "$dir/$file: $!\n";
while (<$FILE>) {
print $out $_ if /^<string to search>/;
}
close $FILE;
}
close $out;
closedir $DIR2;
}
closedir(DIR);
exit 0;



(This post was edited by FishMonger on Jan 21, 2014, 1:15 PM)


Edit Log:
Post edited by FishMonger (Veteran) on Jan 21, 2014, 1:15 PM


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

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