469,632 Members | 1,631 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Replace string in existing file

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
Jul 19 '05 #1
4 45781
you need remember and reset the file pointer before writing out the
result, right before "print IN".
#! /usr/bin/perl -w

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

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

close IN;


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 4:58pm up 2 days 18:52 load average: 1.00 1.00 1.00
Jul 19 '05 #2
Rich wrote:
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
Jul 19 '05 #3
df***@bee.net (Rich) wrote in message news:<eb**************************@posting.google. com>...
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
Jul 19 '05 #4
df***@bee.net (Rich) wrote in message news:<eb**************************@posting.google. com>...
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;
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Niels Bosboom | last post: by
1 post views Thread by Neil Zanella | last post: by
1 post views Thread by nasirmajor | last post: by
1 post views Thread by shapper | last post: by
3 posts views Thread by mouac01 | last post: by
5 posts views Thread by Unknown | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.