----- Original Message ----- From: <[EMAIL PROTECTED]>
Hi all,

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.

#!/usr/bin/perl
 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;
10
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         }
16
17 closedir DH;
18
19 $Date = `date --date=yesterday +%Y%m%d0000-2400-0`;
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
22 $File_Name = "file111-11-23.abctyu_X.$Date";
23 print $Date;
24 foreach $file_name (@array){
25         chomp;

chomp is not needed here. Results of readdir do not end with a newline


26         if ($file_name =~ /$File_Name/){

Or:            if ($file_name eq $File_Name)


27                 open LOGFILE, $File_Name;
28                 while (<LOGFILE>){
29                         #Search log file for the word file.zip
30                         if(/file.zip/){
31                         print "$_\n";
32                         }
33                 }
34         }
35 }


One thing that probably will cause a problem is 'readdir' returns filenames and directories without the path.

So, when you want to open your file, (line 27), unless the current directory of the program file is the same as the file you wish to open, it won't open it. You need to say like:

open LOGFILE, "$Log_Dir$File_Name" or die "Unable to open $Log_Dir$File_Name $!";

From the documentation for readdir:

If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. Otherwise, because we didn't chdir there, it would have been testing the wrong file.

Chris



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to