SunQuest
           Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 9th, 2007, 10:41 AM
csmaster2005 csmaster2005 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 45 csmaster2005 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 5 m 27 sec
Reputation Power: 4
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.

Reply With Quote
  #2  
Old November 9th, 2007, 11:08 AM
L7Sqr L7Sqr is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 590 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 35 m 41 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
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.

Reply With Quote
  #3  
Old November 9th, 2007, 12:01 PM
csmaster2005 csmaster2005 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 45 csmaster2005 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 5 m 27 sec
Reputation Power: 4
I'm somewhat confused on how to iterate to that line... can you please post code examples?

Reply With Quote
  #4  
Old November 10th, 2007, 06:11 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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

Reply With Quote
  #5  
Old November 17th, 2007, 10:38 AM
sf2k's Avatar
sf2k sf2k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 173 sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 19 m 21 sec
Reputation Power: 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:
Original - 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:
Original - 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

Reply With Quote
  #6  
Old November 17th, 2007, 01:04 PM
L7Sqr L7Sqr is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 590 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 35 m 41 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
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> 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Changing a certain line in txt file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway