Jose Malacara wrote:
>> opendir(DIR, 'data') ...
>
> This allows me to grab the correct file names from within the 'data'
> directory, but I guess that my problem happens when the script
> actually goes to parse those files that it only looks for the
> filenames locally, rather than in the actual 'data' directory.
>
>> while (<INFILE>)...
>
> This give me the correct file called 'logfile1', but I need my INFILE
> to actually be pointing to 'data/logfile1'
Joseph's right. Without specifying a path you're opening ./logfile1 on
handle INFILE. You have two choices: specify a path in your open:
open INFILE, 'data/'.$input;
or move your current working directory to 'data', which is your best
bet if /all/ of your data files (both input and output) are there.
'chdir' will do this for you:
> #!/usr/bin/perl -w
> use strict;
> use Text::CSV;
>
> my $ip=$ARGV[0];
> my $csv=Text::CSV->new();
chdir 'data';
> opendir(DIR, "data"); #<=== 'data' directory contains several log
opendir DIR, '.' or die $!;
> files to be parsed.
> my @files = readdir(DIR);
closedir DIR;
> foreach my $file (@files) {
> my $input="$file";
No need to copy $file, you can use it directly.
> open(INFILE,"$input") || die "Can't open file $input"; #<=== is
open INFILE, $file or die $!;
> opening correct file name, but not 'data/logfile1.
>
>
> while (<INFILE>) {
> chomp;
> if($csv->parse($_)) {
> if (m|$ip|) {
> my @fields=$csv->fields;
> print "$fields[0],$fields[4],$fields[5],$fields[6]\n";
> }
> }
> }
> }
>
> closedir(DIR);
Do this after you've finished reading it.
> close INFILE;
This should work roughly as intended. If you need to know what
your current working directory is,
use Cwd;
which imports subroutine 'cwd', amongst other things, and lets you
do this sort of thing:
my $fullpath = cwd.$basename;
Also you might consider using 'glob' as follows:
my @files = glob 'data/*';
or
chdir 'data';
my @files = glob '*';
You can even miss out all the intermediate 'opendir' call
and temporary variables with this:
chdir 'data';
foreach my $file ( glob '*' ) {
:
}
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]