Adding a line in a file inside many directories

V

Ved

Hi all,
I am a beginer in PERL.(I have posted this message in perl.beginers
also and is with moderators)
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);

#########
 
G

Gunnar Hjalmarsson

Ved said:
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.

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

open (dirIN, "$file_list/kat.s") or ...

But that will only open the file for reading. Furthermore, since you
want to add a line at a specific position in the file, I think you
should consider the advice in this FAQ entry:

perldoc -q "insert a line"
 
V

Ved

Separate the problem into two parts:
1) A loop that goes through the file names, one at a time.
2) A subroutine that deals with the process of editing a single file.

my $dir_list = 'list_of_dir.txt';
my $file_to_change = 'kat.s';
open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
while (my $dir = <$fh>) {
chomp $dir;
my $file = "$dir/$file_to_change";
if (-e $file) {
process_one_file($file);
} else {
warn "File $file does not exist; skipping\n";
}
}

sub process_one_file {
my $file = shift;
print "Processing $file\n";
...
}

Unless you want to deal with the magic of perl's $^I variable (perldoc -q -i),
go with Gunnar's suggestion (perldoc Tie::File) for process_one_file.

-Joe

Hey Guys,
Thanks for the replies.

Joe I tried the code. This is what it is returning
######
File green_mcs020 /kat.s does not exist; skipping
File green_mcs0_40 /kat.s does not exist; skipping
File green_mcs0_20L /kat.s does not exist; skipping
File green_mcs0_40_20U /kat.s does not exist; skipping
#######

I think it is because the whitespace between the directory and file.

Directory file
green_mcs020(whitespace)/kat.s

Am I right ?

I did some chomp in the code but it didn't help. From where this
whitespace is coming in ?
After adding chomps code looks like this now :

#!/usr/local/bin/perl
use strict;
use warnings;

my $dir_list = 'my.tests';
chomp $dir_list ; ## added by me
my $file_to_change = 'katana.sv';
chomp $file_to_change ; ## added by me
open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
while (my $dir = <$fh>) {
chomp $dir;
my $file = "$dir/$file_to_change";
print $file ; #added by me
if (-e $file) {
process_one_file($file);
} else {
warn "File $file does not exist; skipping\n";
}
}


sub process_one_file {
my $file = shift;
print "Processing $file\n";
# ...
}
 
G

Gunnar Hjalmarsson

Ved said:
Joe I tried the code. This is what it is returning
######
File green_mcs020 /kat.s does not exist; skipping
File green_mcs0_40 /kat.s does not exist; skipping
File green_mcs0_20L /kat.s does not exist; skipping
File green_mcs0_40_20U /kat.s does not exist; skipping
#######

I think it is because the whitespace between the directory and file.

Directory file
green_mcs020(whitespace)/kat.s

Am I right ?

Sounds plausible...
I did some chomp in the code but it didn't help. From where this
whitespace is coming in ?

Maybe the file is corrupt. Try to run this from command line:

perl -pi -e 's/\s+$/\n/' my.tests

It should remove redundant trailing whitespace.
After adding chomps code looks like this now :

#!/usr/local/bin/perl
use strict;
use warnings;

my $dir_list = 'my.tests';
chomp $dir_list ; ## added by me
my $file_to_change = 'katana.sv';
chomp $file_to_change ; ## added by me

Please do not just do "some chomp". It's obvious that you don't know
what that function does, so you'd better check the applicable docs:

perldoc -f chomp
 
V

Ved

Hey Guys,
Thanks for the replies.

Joe I tried the code. This is what it is returning
######
File green_mcs020 /kat.s does not exist; skipping
File green_mcs0_40 /kat.s does not exist; skipping
File green_mcs0_20L /kat.s does not exist; skipping
File green_mcs0_40_20U /kat.s does not exist; skipping
#######

I think it is because the whitespace between the directory and file.

Directory file
green_mcs020(whitespace)/kat.s

Am I right ?

I did some chomp in the code but it didn't help. From where this
whitespace is coming in ?
After adding chomps code looks like this now :

#!/usr/local/bin/perl
use strict;
use warnings;

my $dir_list = 'my.tests';
chomp $dir_list ; ## added by me
my $file_to_change = 'katana.sv';
chomp $file_to_change ; ## added by me
open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
while (my $dir = <$fh>) {
chomp $dir;
my $file = "$dir/$file_to_change";
print $file ; #added by me
if (-e $file) {
process_one_file($file);
} else {
warn "File $file does not exist; skipping\n";
}
}

sub process_one_file {
my $file = shift;
print "Processing $file\n";
# ...
}- Hide quoted text -

- Show quoted text -

Ok I did a chop to make it working.
chomp $dir;
chop $dir; #to get rid of whitespace
my $file = "$dir/$file_to_change";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top