473,419 Members | 1,524 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,419 software developers and data experts.

Manipulating text files...

Hi,
I have some text files and all I need is to look for a certain word a
replace it with another one. What is the best way of doing this ?

Thank You very much for any tip,
G.Y.

PS:
I'm trying not to rewrite the entire file but as soon as I get the wanted
word just replace that word and quit from that process...
Nov 17 '05 #1
3 1592
PL
You could easily do it with the Regex class...

PL

"genc_ ymeri at hotmail dot com" <ge********@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi,
I have some text files and all I need is to look for a certain word a
replace it with another one. What is the best way of doing this ?

Thank You very much for any tip,
G.Y.

PS:
I'm trying not to rewrite the entire file but as soon as I get the wanted
word just replace that word and quit from that process...

Nov 17 '05 #2
I wouldn't recommend this, as the Regex class works with strings, not
streams. If the size of the files are large, then you would have to load
the complete contents of the file into a string, and that could kill
performance.

Rather, you should just scan through the bytes and search for the word
yourself. Once you find the first character/byte that matches, you would
determine if that is the word, and then replace it.

When replacing, you are going to have to create a new file, or re-write
the bytes to the original file while scanning through them.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"PL" <pl*****@pleasedontsendspam.mailhot.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
You could easily do it with the Regex class...

PL

"genc_ ymeri at hotmail dot com" <ge********@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi,
I have some text files and all I need is to look for a certain word a
replace it with another one. What is the best way of doing this ?

Thank You very much for any tip,
G.Y.

PS:
I'm trying not to rewrite the entire file but as soon as I get the wanted
word just replace that word and quit from that process...


Nov 17 '05 #3
hi,

This is a pseudo code but will give you a good idea

//open the file
StreamReader reader = new StreamReader( ..... );
//open a temporary file ( use Path.GetTempPath/GetTempFileName for this file
StreamWriter writer = ....
//read a line at a time
string line;
while( (line= stream.ReadLine() ) != null )
//look for the word
if ( line.LastIndexOf( word) != -1 )
writer.WriteLine( line.Replace( word, newword) );

//close streams
reader.Close();
writer.Close();

//overwrite source file
File.Delete( .. .);
File.Copy( tempfile, origfile );

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"genc_ ymeri at hotmail dot com" <ge********@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi,
I have some text files and all I need is to look for a certain word a
replace it with another one. What is the best way of doing this ?

Thank You very much for any tip,
G.Y.

PS:
I'm trying not to rewrite the entire file but as soon as I get the wanted
word just replace that word and quit from that process...

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Segfahlt | last post by:
I have a fairly simple C# program that just needs to open up a fixed width file, convert each record to tab delimited and append a field to the end of it. The input files are between 300M and...
6
by: Tom Rowton | last post by:
This one has me a bit confused and I'm not finding what I need in the MSDN or by searching these forums, so here goes... I have a rather large, complex code-in-page WebForm (don't ask) and a...
2
by: Ido Flatow | last post by:
Hi all, I've been exploring the way I can manipulate WSDL.exe using SchemaImporterExtension in order to create a proxy to my liking. My situation is as follows - I have a web site that has...
1
by: John | last post by:
Hi We have a staff file in which each record has a text field which has the name & path of the staff's picture (stored externally). Over years we have archived several staff but pictures being...
1
by: kenzoyann6 | last post by:
Good Morning to everyone! My name is Yann! I need to create a s/w in VB.Net 2005, in which I need to manipulate data in a text file; that is I need to store, view, edit and delete data in a text...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
1
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit : If the file you're reading is not too big, you can use file.readlines() which read all the files and returns its content as a...
1
by: ajd335 | last post by:
Hey all..I am trying to make as script , which can import one directory called IP , take all the text files one by one from that directory , and then can give me (using pregmatch or something )...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.