#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    47
    Rep Power
    10

    Changing a certain line in txt file


    I need to change a certain line in a text file to be something else.
    For example, how would I go to the 15th line in a file i've opened and change it to be something like "this is the new 15th line" instead?

    Any help is appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    You have many choices.
    Any of teh following could work:
    * IO.each
    * IO.each_with_index
    * IO.gets
    * IO.read
    * IO.readlines
    * IO.each_line
    * IO.pos=
    * IO.collect

    Try some. Post back with problems.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    47
    Rep Power
    10
    I'm somewhat confused on how to iterate to that line... can you please post code examples?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    The File object has an .each method as well as a .readlines method. You can use either one.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    As an exercise for the reader (me) I tried the following:

    I assumed I can just use readlines and that works fine. Here I loaded the file test.txt and then select the seventh line from f. (offset by zero)

    ruby Code:
     
     
    irb(main):006:0> f=File.open("test.txt").readlines
    => ["this\n", "is\n", "a\n", "test\n", "of\n", "the\n", "emergency\n", "broadcast\n", "system"]
    irb(main):007:0> f[6]
    => "emergency\n"


    However I tried to use f.each for the file but not sure of it's equivalent as the docs aren't clear.

    ruby Code:
     
     
    irb(main):038:0> f.each {|line|
    irb(main):039:1*   if f.lineno ==7:
    irb(main):040:2*     puts line
    irb(main):041:2>   end
    irb(main):042:1> }
    => #<File:test.txt>
    irb(main):043:0>


    Normally I'd just use readlines, but how would the above f.each method give me the text of the seventh line number?

    cheers
    sf2k
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Code:
    irb(main):001:0> x = File.open("new_file")
    => #<File:new_file>
    irb(main):002:0> x.each { |l| puts l }
    Line number 1
    Line number 2
    Line number 3
    Line number 4
    Line number 5
    Line number 6
    => #<File:new_file>
    irb(main):003:0> x.each { |l| puts l }
    => #<File:new_file>
    irb(main):004:0> x = File.open("new_file")
    => #<File:new_file>
    irb(main):005:0> x.each { |l| puts l }
    Line number 1
    Line number 2
    Line number 3
    Line number 4
    Line number 5
    Line number 6
    => #<File:new_file>
    irb(main):006:0>
    You have to reload the file again. Or you could do something like:
    Code:
    irb(main):001:0> x = File.open("new_file")
    => #<File:new_file>
    irb(main):002:0> x.each { |l| puts l }
    Line number 1
    Line number 2
    Line number 3
    Line number 4
    Line number 5
    Line number 6
    => #<File:new_file>
    irb(main):003:0> x.rewind
    => 0
    irb(main):004:0> x.each { |l| puts l }
    Line number 1
    Line number 2
    Line number 3
    Line number 4
    Line number 5
    Line number 6
    => #<File:new_file>
    irb(main):005:0>
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo