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



Kenosis
User

Apr 11, 2013, 9:13 AM


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

Here's another option:


Code
use strict; 
use warnings;

local @ARGV = grep -r, </var/log/secure.*> or die "No files found to process.\n";

open my $out, '>', 'data.txt' or die $!;

while (<>) {
print $out $_ if /Failed/;
}


Consider using a glob to obtain the list of files you want, and then grep each file with -r, passing the results to a local copy of @ARGV, so Perl handles the i/o on those files (as you originally had). Opening the data file for appending isn't necessary, since any results will be immediately written to the file. Closing the data file is optional in this case, as it'll automatically close when the script ends.

You said:


Quote
i cant able to identify what mistake i did in my script and why its not working.


Your code:


Code
while(<>){  
open (MYFILE, '>>data.txt');
print MYFILE if (/Failed/);
}


It's problematic that you're opening the data file each time a log line is read. Also, you're not printing anything to the opened file, but were printing the value of the file handle. Didn't you see anything like GLOB(0x85e998)GLOB(0x85e998)GLOB(0x85e998) when your script was running?

Hope this helps!


(This post was edited by Kenosis on Apr 11, 2013, 1:06 PM)


Edit Log:
Post edited by Kenosis (User) on Apr 11, 2013, 9:14 AM
Post edited by Kenosis (User) on Apr 11, 2013, 9:15 AM
Post edited by Kenosis (User) on Apr 11, 2013, 9:16 AM
Post edited by Kenosis (User) on Apr 11, 2013, 9:17 AM
Post edited by Kenosis (User) on Apr 11, 2013, 9:23 AM
Post edited by Kenosis (User) on Apr 11, 2013, 9:28 AM
Post edited by Kenosis (User) on Apr 11, 2013, 10:13 AM
Post edited by Kenosis (User) on Apr 11, 2013, 10:20 AM
Post edited by Kenosis (User) on Apr 11, 2013, 10:20 AM
Post edited by Kenosis (User) on Apr 11, 2013, 1:06 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