Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding and writing to specified area in a text file

Reply
Thread Tools

Finding and writing to specified area in a text file

 
 
Ryan Mckenzie
Guest
Posts: n/a
 
      11-14-2008
Hello,

I'm trying to find a specified keyword in a text file that I have open
then replace the line of that keyword with some new text. Is it
possible do do this?

So something like:

#custom.txt
hello world
INSERT
end of hello world

So INSERT would be swapped with some new text.

Thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Mckenzie
Guest
Posts: n/a
 
      11-14-2008
Aldric Giacomoni wrote:
> In all of my research online, the only way that I've found to do it is
> to read the entire file, replace what you wanna replace, write to a new
> file, then copy over the original one. I hope someone can show me a
> better way.
>
> --Aldric


Hi Aldric,

I'm actually looking to replace some text/code in 3-4 places so i can't
hardcode specific lines into the model because they could change. All
of this needs to be done on a button click so the actual file is hidden
from the user. I'm eventually wanting to add form input boxes and use
them to replace the text/code areas.

Thanks for your help. Please could you put up some of your code for me
to study?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Michael Tomer
Guest
Posts: n/a
 
      11-20-2008
A gsub (global substitution) should take care of the problem. Here's an
example of how I might do this:

################################################## ####

data = ""

File.open("my_file.txt", "r") do |file|
data = file.read.gsub("INSERT", "new word")
end

File.open("my_file.txt", "w") do |file|
data = file.write(data)
end

################################################## ####

The gsub command also supports regular expressions, if you need it.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
George Opritescu
Guest
Posts: n/a
 
      11-20-2008
Aldric Giacomoni wrote:
> In all of my research online, the only way that I've found to do it is
> to read the entire file, replace what you wanna replace, write to a new
> file, then copy over the original one. I hope someone can show me a
> better way.
>
> --Aldric


You know , there is an alternative to writing to a new file . You will
have to read the file contents , modify them , seek to the file's start
, write the modified contents and truncate at the current position .
--
Posted via http://www.ruby-forum.com/.

 
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
do structure definitions go in data area or in code area... hotadvice C Programming 14 10-02-2007 04:10 AM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
Add point to graphics area without having to paint the whole area? Mark Java 1 06-27-2005 03:09 AM
reading UTF-8 lines and writing to TK text area Matthew Huggett Ruby 2 02-15-2004 01:22 PM
Writing text to a specific area in a control Grant Mills ASP .Net Web Controls 3 01-15-2004 06:47 AM



Advertisments