[EMAIL PROTECTED] wrote:
Hi all,
Hello,
Serious noob here (one week into llama 5th ed.).
I'm trying to write a script to pull a specific file from a directory
and search that file for specific phrases. I am having a problem
searching for the file in the directory. If I type in the actual file
name (line 26) I can find the phrase file.zip (line 30). Obviously,
this will only work once. If I use a variable to search for a file I
get nothing. What is the proper format for line 26? I can't use
File::Find so please don't suggest it. Thx.
Why can't you use File::Find? And why would you want to use it for this
application?
#!/usr/bin/perl
use warnings;
2 use strict;
3
4 my $Log_Dir;
5 my $file;
6 my $Date;
7 my $File_Name;
8 my @array;
9 my $file_name;
Why are you declaring all your variables here?
11 $Log_Dir = "/var/log/apache/";
12 opendir DH, $Log_Dir or die "Cannot open $Log_Dir: $!";
13 while ($file = readdir DH){
14 push(@array, $file);
15 }
Why the while loop?
my @array = readdir DH;
Why is the array named "array"?
17 closedir DH;
18
19 $Date = `date --date=yesterday +%Y%m%d0000-2400-0`;
Assuming today's date is 21 Oct. 2008, $Date will now contain the string
"200810200000-2400-0\n"
perldoc -q "How do I find yesterday.s date"
20 # The file name always starts with file111-11-23.abctyu_X but the
time stamp changes daily (predictable)
21 # Example Filename: file111-11-23.abctyu_X.200810200000-2400-0
It looks like your file name does not have a "\n" at the end?
22 $File_Name = "file111-11-23.abctyu_X.$Date";
23 print $Date;
24 foreach $file_name (@array){
25 chomp;
Why are you chomp()ing $_?
26 if ($file_name =~ /$File_Name/){
Why use a regular expression?
if ( $file_name eq $File_Name ) {
27 open LOGFILE, $File_Name;
You should *always* verify that the file opened correctly.
28 while (<LOGFILE>){
29 #Search log file for the word file.zip
30 if(/file.zip/){
The . character in a regular expression matches every character (except
newline) so you have to escape it to match a literal . character:
if ( /file\.zip/ ) {
31 print "$_\n";
32 }
33 }
34 }
35 }
You are reading all the file names from a directory to find a file that
you already know the name of. You probably want some like this instead:
#!/usr/bin/perl
use warnings;
use strict;
my $log_dir = '/var/log/apache';
chomp( my $yesterday = `date --date=yesterday +%Y%m%d` );
my $file_name = "$log_dir/file111-11-23.abctyu_X.${yesterday}0000-2400-0";
open my $LOGFILE, '<', $file_name or die "Cannot open '$file_name' $!";
while ( <$LOGFILE> ) {
#Search log file for the word file.zip
print if /file\.zip/;
}
__END__
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/