Re: Grep
- From: "DJ Stunks" <DJStunks@xxxxxxxxx>
- Date: 26 Nov 2006 20:24:42 -0800
bill@xxxxxxxxxxxxx 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
.
- References:
- Grep
- From: bill@xxxxxxxxxxxxx
- Grep
- Prev by Date: Re: Fixing an apache2 perl module
- Next by Date: Re: Printing one txt file to a new txt file backwards
- Previous by thread: Grep
- Index(es):
Relevant Pages
|
|