Home > Archive > PERL Beginners > June 2007 > Adding a line in a file inside many directories
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Adding a line in a file inside many directories
|
|
|
| Hi all,
I am a beginer in PERL.
What I am trying to do is this:
I have a 150 directories, each having a file "kat.s" in them.
I have names of all these directories in a text file
"list_of_dir.txt".
I have to open the each of "kat.s" in all the 150 directories and add
a line(say "ABCD" at line number 20) in each of them.
What I have thought of doing is that using "list_of_dir", open each
directory and open kat.s and print the required statment.
I have written a code. But I am stuck in opening the directory and
than kat.s file.
Please help.
(Or suggest any better way to do it)
Thanks.
#######
use strict;
use warnings;
my $file = 'print.txt';
open my $VEDOUT, '>', $file or die "Could not open '$file': ";
open (VEDIN, 'list_of_dir.txt') or die "Cannot open 'File.txt' $!";
my @rgstr=<VEDIN>;
foreach my $file_list (@rgstr) {
print $file_list ; #printing list of dir
open (dirIN, '$file_list/kat.s') or die "Cannot open 'File.txt' $!";
#This is not working
}
close (VEDIN);
close ($VEDOUT);
#########
| |
| Tom Phoenix 2007-06-29, 9:58 pm |
| On 6/28/07, Ved <vedpsingh@gmail.com> wrote:
> open (VEDIN, 'list_of_dir.txt') or die "Cannot open 'File.txt' $!";
Why does the error message mention the wrong file name?
> my @rgstr=<VEDIN>;
>
> foreach my $file_list (@rgstr) {
> print $file_list ; #printing list of dir
The items in this list haven't been chomped, have they? So each item
ends with a newline.
> open (dirIN, '$file_list/kat.s') or die "Cannot open 'File.txt' $!";
> #This is not working
If $file_list ends with a newline, I wouldn't expect it to work. Make
sure you're trying to open the correct filename, and make sure that
you mention the correct filename in the error message from die.
Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|