Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Replace string in existing file

Reply
Thread Tools

Replace string in existing file

 
 
Rich
Guest
Posts: n/a
 
      02-19-2004
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
 
Reply With Quote
 
 
 
 
toylet
Guest
Posts: n/a
 
      02-19-2004
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
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      02-20-2004
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
 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      02-21-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Rich) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      02-22-2004
(E-Mail Removed) (Rich) wrote in message news:<(E-Mail Removed). 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;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Existing Dll - using Functions from an existing dll Tristin.Colby@gmail.com Ruby 0 02-05-2008 07:38 PM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
Why no existing Java type to existing XML schema binding support? nrm Java 3 04-10-2006 04:52 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments