|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#3
|
|||
|
|||
|
I'm somewhat confused on how to iterate to that line... can you please post code examples?
|
|
#4
|
||||
|
||||
|
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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#5
|
||||||||
|
||||||||
|
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:
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:
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 |
|
#6
|
|||
|
|||
|
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>
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>
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Changing a certain line in txt file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|