Replace string in existing file

R

Rich

I am trying to replace the word "test" in a file with the word
"Change" without creating a new file using the following code:

#! /usr/bin/perl -w

open (IN, "+</path/to/file/test.html");

while (<IN>){
s/test/Change/;
print IN;
}

close IN;

The orignal file contains:

test 1

test 2

test 3

and my results are:

test 1

test 2

test 3
test 1
Change 1
test 3
Change 3

I am looking for the results to be:

Change 1

Change 2

Change 3

and that is what I get if I output the print to a new file or to
<STDOUT>.

What am I doing wrong? Any help would be much appreciated.

Thanks in advance
 
T

toylet

you need remember and reset the file pointer before writing out the
result, right before "print IN".
 
J

Joe Smith

Rich said:
I am trying to replace the word "test" in a file with the word
"Change" without creating a new file using the following code:

open (IN, "+</path/to/file/test.html");
while (<IN>){
s/test/Change/;
print IN;
}
close IN;

The obvious problem is that you're not using seek().

The not so obvious problem is that you're doomed to failure if the
file is longer than the standard I/O buffer.

If the input is like "test 19\n", then you'll be changing eight
character lines to ten character lines. After processing four
lines (32 bytes), you will have written four lines (40 bytes), and
the fifth line read will consist of nothing but overwritten bytes.

You may have to consider reading the entire file into memory,
performing the changes, seek to the beginning, print the data
all at once, and truncating the file to size (if it got smaller).
-Joe
 
R

Rich

I am trying to replace the word "test" in a file with the word
"Change" without creating a new file using the following code:

#! /usr/bin/perl -w

open (IN, "+</path/to/file/test.html");

while (<IN>){
s/test/Change/;
print IN;
}

close IN;

The orignal file contains:

test 1

test 2

test 3

and my results are:

test 1

test 2

test 3
test 1
Change 1
test 3
Change 3

I am looking for the results to be:

Change 1

Change 2

Change 3

and that is what I get if I output the print to a new file or to
<STDOUT>.

What am I doing wrong? Any help would be much appreciated.

Thanks in advance


Thank you both for the tips. I was not aware of the seek() function
and I am currently trying to learn how to use it correctly but I think
I'm on the right track. I will post my results.

Thanks - Rich
 
R

Rich

I am trying to replace the word "test" in a file with the word
"Change" without creating a new file using the following code:

#! /usr/bin/perl -w

open (IN, "+</path/to/file/test.html");

while (<IN>){
s/test/Change/;
print IN;
}

close IN;

The orignal file contains:

test 1

test 2

test 3

and my results are:

test 1

test 2

test 3
test 1
Change 1
test 3
Change 3

I am looking for the results to be:

Change 1

Change 2

Change 3

and that is what I get if I output the print to a new file or to
<STDOUT>.

What am I doing wrong? Any help would be much appreciated.

Thanks in advance


Got it! I had to hold the text in an array first though.

#! /usr/bin/perl -w

open (IN, "+</path/to/file/test.html");

@file = <IN>;

seek IN,0,0;

foreach $file (@file){
$file =~ s/test/Change/g;
print IN $file;
}
close IN;
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top