For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Grep









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Grep
bill@randomdog.net

2006-11-26, 9:56 pm

Hi All,

I'm new to PERL so please forgive this posting...

I have a file that I'm trying to "grep" a bunch of things with.
There are multiple entries in the file (system.log).

For example... I'm looking to grab all the "Failed Authentications" and
place them into a file and periodically email me the results.

I've got the opening of the file OK.
I've been placing the contents into an array...OK

When I run my script all that I am getting back is the last line that
contains what I'm looking for.

Can someone show me the light? What is the correct syntax for what I'm
looking to do.

Thanks in advance.

Bill

#!/usr/bin/perl


my $sys_log="/var/log/system.log";


open (SYSTEMLOG, "$sys_log") or die "Error opening opening logfle:$!";



#Initialize our main variables


my $now=localtime;

my $failed = "";

my $dirserve = "";




#Go over log line by line, storing the current line in the $line variable



for my $line (<SYSTEMLOG> ){



#remove trailing returns


chomp $line;



if ($line =~/Failed Authentications/){
$hostname = $line;

}


if ($line =~/DirectoryService/){
$dirserve = $line;
}

}



# Close open file handle;
close SYSTEMLOG;



#We've already grabbed the varaibles

#Now we're going to write it to a file.



my $dum_log="/Users/bill/Desktop/dummy.log";


open (DUMMY, ">>$dum_log") or die "Error opening opening logfle:$!";



print DUMMY "$now \n";

print DUMMY "$hostname \n";

print DUMMY "$dirserve \n";



close DUMMY;




DJ Stunks

2006-11-27, 3:57 am

bill@randomdog.net wrote:
> Hi All,
>
> I'm new to PERL so please forgive this posting...


Perl please.... not PERL :)

> I have a file that I'm trying to "grep" a bunch of things with.
> There are multiple entries in the file (system.log).
>
> For example... I'm looking to grab all the "Failed Authentications" and
> place them into a file and periodically email me the results.
>
> I've got the opening of the file OK.
> I've been placing the contents into an array...OK
>
> When I run my script all that I am getting back is the last line that
> contains what I'm looking for.
>
> Can someone show me the light? What is the correct syntax for what I'm
> looking to do.


your issue isn't syntax related so far as I can tell (at first blush it
looks fine). the error is in your implementation of your solution...


> #!/usr/bin/perl
>
> my $sys_log="/var/log/system.log";
>
> open (SYSTEMLOG, "$sys_log") or die "Error opening opening logfle:$!";
>
> my $now=localtime;
> my $failed = "";
> my $dirserve = "";
>
> for my $line (<SYSTEMLOG> ){
>
> chomp $line;
>
> if ($line =~/Failed Authentications/){
> $hostname = $line;
> }


here, you overwrite whatever what was in $hostname with what is in
line....

> if ($line =~/DirectoryService/){
> $dirserve = $line;
> }


same here with $dirserve

> }
>
> # Close open file handle;
> close SYSTEMLOG;
>
> my $dum_log="/Users/bill/Desktop/dummy.log";
>
> open (DUMMY, ">>$dum_log") or die "Error opening opening logfle:$!";
>
> print DUMMY "$now \n";
> print DUMMY "$hostname \n";
> print DUMMY "$dirserve \n";


now you print out the current contents of the vars...

> close DUMMY;


to repair your script I would suggest either

1) pushing $lines onto @hostnames & @dirserve arrays and
then printing the arrays into your file at the end; or
2) open your output file before you loop through your syslog,
and as you find interesting lines write them out one by one

the difference between the approaches is that the first gives you more
control over how you format or layout your hostnames and dirserves.

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com