473,389 Members | 1,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

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 45975
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Niels Bosboom | last post by:
Hi! I am using PHP4.3.3 on a windows XP platform. I am building a small program that will allow my students to subscribe and unsubscribe from a service that will let them know when new results...
2
by: Nico Grubert | last post by:
Hi there, I would like to open an existing file that contains some lines of text in order to append a new line at the end of the content. My first try was: >>> f = open('/tmp/myfile', 'w')...
1
by: Neil Zanella | last post by:
Hello, The MSDN documentation does not seem to mention whether the Replace string method returns a new string or modifies the existing string in place and then returns it. I would like to know...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
by: nasirmajor | last post by:
Dear all i have the following code for writing to the existing file. =========================== void AppendToFileEng() { SqlDataReader gr = null; string engpath2=""; gr =...
1
by: shapper | last post by:
Hello, I have a XLST file where I have the phrase "HERE". Something like: <xsl:text>HERE</xsl:text> I want to replace "HERE" by "UPDATED" and save the file.
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
5
by: Unknown | last post by:
Hi, I've got this code : cb = open("testfile", "r+") f = cb.readlines() for line in f: rx = re.match(r'^\s*(\d+).*', line) if not rx: continue else:
1
by: mukeshrasm | last post by:
Hi I want to delete or replace the existing file from directory using PHP. I don't want to rename the file.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.