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:
Overwrite is not working?

 



anishkumarv
Novice

Apr 11, 2013, 5:04 AM

Post #1 of 10 (443 views)
Overwrite is not working? Can't Post

Hi all,

I am totally newbie to perl and this is simple script to get failed attempts from /var/log/secure

In that i can't able to overwrite the output (>) in a file data.txt but i can able to append (>>)

What mistake iam doing ? kindly guide me to resolve this issue.


Code
#!/usr/bin/perl 
use strict;
use warnings;
use diagnostics;
chdir("/var/log") or die "$!";
opendir (DIR, ".") or die "$!";
my @files = grep {/secure*?\.*/} readdir DIR;
close DIR;
{
local @ARGV = @files;
while(<>){
open (MYFILE, '>>data.txt');
print MYFILE if (/Failed/);
}
}


With Regards
Anish Kumar.V


g4143
Novice

Apr 11, 2013, 5:13 AM

Post #2 of 10 (440 views)
Re: [anishkumarv] Overwrite is not working? [In reply to] Can't Post

Do you have permission to read data in /var/log or do you have permission to create files in /var/log?


(This post was edited by g4143 on Apr 11, 2013, 5:15 AM)


anishkumarv
Novice

Apr 11, 2013, 5:25 AM

Post #3 of 10 (435 views)
Re: [g4143] Overwrite is not working? [In reply to] Can't Post

yes i have permission if permission is the problem i can't able to append also right? but i can able to append only overwrite is the problem.

Thanks for the quick reply.


g4143
Novice

Apr 11, 2013, 5:38 AM

Post #4 of 10 (432 views)
Re: [anishkumarv] Overwrite is not working? [In reply to] Can't Post

I noted that this


Code
close DIR;


should be


Code
closedir DIR;


I logged in as root and tried this and it worked.


Code
#!/usr/bin/perl 
use strict;
use warnings;
use autodie qw/open close/;
use diagnostics;

chdir("/var/log") or die "$!";
opendir (my $DIR, ".") or die "$!";

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

open(my $OFILE, ">>", "data.txt");

foreach( @files )
{
if ( -r $_ )
{
open(my $IFILE, "<", $_);

while ( <$IFILE> )
{
print {$OFILE} $_;
}

close($IFILE);
}
}

close($OFILE);

__END__



(This post was edited by g4143 on Apr 11, 2013, 5:40 AM)


anishkumarv
Novice

Apr 11, 2013, 5:57 AM

Post #5 of 10 (426 views)
Re: [g4143] Overwrite is not working? [In reply to] Can't Post

Hi ,

My requirement is need to get only the failed login attempts from /var/log/secure
and stored that output into a file. but your script didn't do that process. and i don't know why you are using this module.

Quote
use autodie qw/open close/;


Without module also we can do this process i hope, and using my script i can able to append only overwrite not working.


FishMonger
Veteran / Moderator

Apr 11, 2013, 6:31 AM

Post #6 of 10 (420 views)
Re: [anishkumarv] Overwrite is not working? [In reply to] Can't Post


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)


anishkumarv
Novice

Apr 11, 2013, 7:10 AM

Post #7 of 10 (412 views)
Re: [FishMonger] Overwrite is not working? [In reply to] Can't Post

Thank a lot its working. but i cant able to identify what mistake i did in my script and why its not working.

any how really thanks a lot guys for your help.

With Regards
Anish kumar.V


Kenosis
User

Apr 11, 2013, 9:13 AM

Post #8 of 10 (404 views)
Re: [anishkumarv] Overwrite is not working? [In reply to] Can't Post

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)


Laurent_R
Veteran / Moderator

Apr 11, 2013, 10:51 AM

Post #9 of 10 (385 views)
Re: [anishkumarv] Overwrite is not working? [In reply to] Can't Post

The main problem in your script was, I think, that you were opening the file over and over again in the while loop. Put the open statement outside of the while loop.


anishkumarv
Novice

Apr 11, 2013, 11:41 PM

Post #10 of 10 (373 views)
Re: [Laurent_R] Overwrite is not working? [In reply to] Can't Post

Thanks a lot for the quick reply , as a beginner in Perl its really very helpful to identify my mistakes and correct that.

With Regards
Anish Kumar.V

 
 


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

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