Finding and writing to specified area in a text file

Discussion in 'Ruby' started by Ryan Mckenzie, Nov 14, 2008.

  1. 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/.
     
    Ryan Mckenzie, Nov 14, 2008
    #1
    1. Advertising

  2. 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/.
     
    Ryan Mckenzie, Nov 14, 2008
    #2
    1. Advertising

  3. 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/.
     
    Michael Tomer, Nov 20, 2008
    #3
  4. 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/.
     
    George Opritescu, Nov 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    611
    Larry Barowski
    Jun 27, 2005
  2. hotadvice
    Replies:
    14
    Views:
    709
    hotadvice
    Oct 2, 2007
  3. Andy B.
    Replies:
    0
    Views:
    813
    Andy B.
    Apr 30, 2010
  4. leo
    Replies:
    1
    Views:
    289
    Bob Lehmann
    Dec 5, 2005
  5. Matthew Huggett
    Replies:
    2
    Views:
    207
    Hidetoshi NAGAI
    Feb 15, 2004
Loading...

Share This Page