Hi i am trying to write a perl program where i have to open a
1)directory "unit"
2) rename the files in the dir say file1.txt;file2.txt...file5.txt
to file1_a.txt;file2_a.txt,....file5_a.txt ;file1_x.txt ;file2_x.txt
3) open these renamed files and replace the words
lets say file1_a.txt has something like this
-------------------------------------
hi...xyz
file1
file2_x
file1_x
file5
hello there
-------------------------------------
the program should search for file1,file2,file3,file4,file5 and file2_x and file1_x and replace it with file1_a or file2_a ,file1_x_a and file2_x_a.
its should not do something like this to the data file2_a_x or file2_a_x_a or file1_a_x or file1_a_x
What you've got so far? Because based on your description it suspiciously sounds like homework.
A good start would be looking at the description of the Perl functions opendir, readdir, glob, and rename (either with perldoc -f <function> or online)
:p... its not an home work :)....I did open dir and rename the files in dir. The only problem i have is with data in the files that i opened to be not over written... example file1_x should not be replace as file1_a_x or file1_a_x_a....I tried to explain the issue in deatiled manner so i had given all the details... i will upload whta i have in a bit...
here is what i have so far
$dirname = "all";
print "The dir that we are processing is $dirname :\n";
#######################################################################
##########Open a Directory called all and cp each unit in it to########
#example dir to dir_a###############################################
########################################################################
opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
foreach $file(readdir DIR)
{
next if $file =~ /^\.\.?$/;
`cp -r $dirname/$file $dirname/${file}_a`;
$dirname1 = "$dirname/${file}_a";
##############################################################################
##########Open a Directory just named dir_a and rename all files in it########
#example file1.txt to file1_a.txt or file2.txt to file2_a.txt file1_x.txt to file1_x_a.txt
##############################################################################
opendir(DIR1, $dirname1) or die "can't opendir $dirname1: $!";
foreach $file1(readdir DIR1)
{
next if $file1 =~ /^\.\.?$/;
#printo
$file1=~/(.*)\.([^\.\t ]+)$/ ;
$file_name=$1;
$file_ext=$2;
print "$dirname1/$file1\n";
print "$dirname1/${file_name}_a.$file_ext\n";
`mv $dirname1/$file1 $dirname1/${file_name}_a.$file_ext `;
@newfile_list="$file_name";
$newfile="$dirname1/${file_name}_a.$file_ext";
#####################################################################################
##########Open a file that was just named file1_a.txt and rename the the words in it that say file1 with file1_a(should not dosomething like
file1_a_a) and file1_x to file1_x_a (it should not do something like this file1_a_x or file1_a_x_a)
######################################################################################
foreach $newfile_list (@newfile_list)
#while(@newfile_list)
{
open(afile, "$newfile")or die " Can not open $newfile \n";
open(afile1, ">$newfile.tmp")or die " Can not open $newfile.tmp \n";
while(<afile>)
{
if(/$newfile_list/)
{
$_=~s/$newfile_list/${newfile_list}_a/ ;
#print afile1 $_;
}
print afile1 $_;
}
close(afile)or die "can't close $newfile : $!";
close(afile1)or die "can't close $newfile.tmp: $!";
#`cp $newfile ${newfile}_org`;
`cp $newfile.tmp $newfile`;
`rm $newfile.tmp `;
}
}
print"\n@newfile_list";
#}
closedir(DIR1);
}
closedir(DIR);
The problem that i ahve is iam overwrite the words..which i dont want to...