Satish Vadlamani wrote:
> I have a directory in which I have about sixty files. Of
> these say 30 have a .ctl extension. The 2nd or 3rd line in
> the files that have a .ctl extension reads something like this
> in all files:
>
> INFILE '/u/svadlama/eCAMPUS/df4.3_2/prototype1/data/buckets.dat'
>
> the text after the word INFILE could be different.
>
> I want to replace the text after the INFILE with
> '/u/svadlama/toysrus/filename.dat'
This looks like a good job for -i. See `perldoc perlrun` for the -i switch
and `perldoc perlvar` for $^I. Basically, you can fill @ARGV with a list of
files to process, and then loop with <>, make any changes you need, and then
write out the line. The file will be changed and the previous version backed
up. You can find out the current filename with $ARGV.
> #!c:\perl
> $dirname="c:\toysrus\data_df1"
> opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
> while (defined($file = readdir(DIR))) {
>
> $file_2=$file;
>
> $file_2=s/\. ...//;
> $_=s#\s* INFILE *#"INFILE /u/svadlama/toysrus/"."$file_2"."dat"#
> }
> closedir(DIR);
This will loop through the directory "c:<tab>oysrusdata_df1" (do you really
have a directory with a tab in its name?) and assign $file to $file_2. It'll
then overwrite that value with the result of replacing a dot, a space, and
three characters with blank in $_ -- since you don't fill $_, you'll end up
with $file_2 being '' since the substitution will return a false value.
Then, you replace "zero or more whitespace chars, one space, 'INFILE', zero
or more spaces" with the text (including the quotes) "INFILE
/u/svadlama/toysrus/".""."dat" -- since $file_2 is blank as explained before
-- in $_ and assign the return value of this substitution (the empty string,
'') to $_.
And you didn't use -w or use strict to avert you of possible problems.
Try this:
#!c:\perl -w
use strict;
my $dirname='c:/toysrus/data_df1'; # use forward slashes
# to avoid having to write \\
opendir DIR, $dirname or die "Can't open $dirname: $!";
@ARGV = grep /\.ctl$/, readdir DIR; # fill @ARGV with *.ctl
closedir DIR or die "Can't close $dirname: $!";
$^I = '.bak'; # save backups in .bak files
while(<>) {
(my $datname = $ARGV) =~ s/\.ctl$/.dat/;
s<^INFILE .*><INFILE /u/svadlama/toysrus/$datname>;
}
Hope this helps.
Cheers,
Philip
---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
[EMAIL PROTECTED]
For non-automated Mailing List support, send email to
[EMAIL PROTECTED]