Sponsored Content
Top Forums Shell Programming and Scripting Replace aword in a.The replaced word should not be overwitten in perl(details inside) Post 302289793 by madhul2002 on Friday 20th of February 2009 01:01:03 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
COPY(3) 								 1								   COPY(3)

copy - Copies file

SYNOPSIS
bool copy (string $source, string $dest, [resource $context]) DESCRIPTION
Makes a copy of the file $source to $dest. If you wish to move a file, use the rename(3) function. PARAMETERS
o $source - Path to the source file. o $dest - The destination path. If $dest is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files. Warning If the destination file already exists, it will be overwritten. o $context - A valid context resource created with stream_context_create(3). RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.4 | | | | | | | Changed the $context parameter to actually have | | | an effect. Previously, any $context was ignored. | | | | | 5.3.0 | | | | | | | Added context support. | | | | | 4.3.0 | | | | | | | Both $source and $dest may now be URLs if the | | | "fopen wrappers" have been enabled. See fopen(3) | | | for more details. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 copy(3) example <?php $file = 'example.txt'; $newfile = 'example.txt.bak'; if (!copy($file, $newfile)) { echo "failed to copy $file... "; } ?> SEE ALSO
move_uploaded_file(3), rename(3), The section of the manual about handling file uploads. PHP Documentation Group COPY(3)
All times are GMT -4. The time now is 07:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy