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: Beginner: Re: [anishkumarv] Overwrite is not working?: Edit Log



FishMonger
Veteran / Moderator

Apr 11, 2013, 6:31 AM


Views: 442
Re: [anishkumarv] Overwrite is not working?


Code
#!/usr/bin/perl 

use strict;
use warnings;

chdir('/var/log') or die "Failed to chdir to /var/log <$!>";
opendir (my $DIR, ".") or die "Failed to opendir . <$!>";

my @files = grep {/secure.*/} readdir $DIR;
closedir $DIR;

open my $out_fh, '>>', 'data.txt' or die "Failed to open 'data.txt' <$!>";

foreach my $file ( @files ) {
-r $file or next;
open my $in_fh, '<', $file or die "Failed to open '$file' <$!>";
while (my $line = <$in_fh>) {
next unless $line =~ /Failed/;
print {$out_fh} $line;
}

close $in_fh;
}

close $out_fh;


BTW, the while loop can be replaced with this single line.

Code
print {$out_fh} grep {/Failed/} <$in_fh>;



(This post was edited by FishMonger on Apr 11, 2013, 6:34 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Apr 11, 2013, 6:34 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