Replace aword in a.The replaced word should not be overwitten in perl(details inside)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace aword in a.The replaced word should not be overwitten in perl(details inside)
# 1  
Old 02-20-2009
Replace aword in a.The replaced word should not be overwitten in perl(details inside)

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

Please help me with this..

let me know if you have more questions
# 2  
Old 02-20-2009
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)
# 3  
Old 02-20-2009
Smilie... its not an home work Smilie....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


Code:
$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...

Last edited by DukeNuke2; 02-20-2009 at 03:24 PM..
# 4  
Old 02-20-2009
then take out the substitution:

$_=~s/$newfile_list/${newfile_list}_a/ ;
# 5  
Old 02-20-2009
how will i replace $newfile_list then?
# 6  
Old 02-20-2009
Sorry, maybe I misunderstood, try using the word boundary anchor (\b) in the regexp in the search side:

Code:
$_=~s/\b$newfile_list\b/${newfile_list}_a/

# 7  
Old 02-21-2009
Thanks...it helped...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with compare 2 column content and corrected/replaced word

Input File CGGCGCCTCGCNNNCGAGCG CGGCGCGCCGAATCCGTGCG TCGCNGC GCGCCGC ACGGCNNNNN ACGGCCTCGCG CGGCNGCCCGCCC CGGCGCGCCGTCC Desired Output File CGGCGCCTCGCNNNCGAGCG CGGCGCGCCGAATCCGTGCG CGGCGCCTCGCATCCGAGCG TCGCNGC GCGCCGC TCGCCGC ACGGCNNNNN ACGGCCTCGCG ACGGCTCGCG... (6 Replies)
Discussion started by: perl_beginner
6 Replies

2. UNIX for Beginners Questions & Answers

Replace exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

3. UNIX for Advanced & Expert Users

Perl command to replace word in file...

Hi, I want to search for a specific word in file and replace whole line with new text. e.g. 1) I have file with below lines APP=ABCD 12/12/2012 DB=DDB 01/01/2013 I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT Simlarly need a... (2 Replies)
Discussion started by: mgpatil31
2 Replies

4. Red Hat

Need to mount LUN on two linux boxs (one rw the other ro) caveat details inside

Calling all Linux GURU's. The purpose of this thread is to try an recreate what we already have stood up in my environment, however the steps i am having to produce my own. I have got multiple applications that dump the data to various LUNs, the LUNs are managed by an Enterprise SAN... (11 Replies)
Discussion started by: jcejka
11 Replies

5. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

6. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

7. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

8. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

9. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

10. Shell Programming and Scripting

Perl log parcer. (for custom logs details inside)

OS:sol8 ok 2 things are very important. I will give examples of the logs and the client file for testing. Object: to display on the web sometime to parse your custom logs basied on the inputed date and client selected. I know alot of ppl are like who care. but i saw a perl script on here... (1 Reply)
Discussion started by: Optimus_P
1 Replies
Login or Register to Ask a Question