LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2005, 07:15 AM   #1
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Rep: Reputation: 47
Ruby file writing


I'am writing a script in Ruby to help me automate some tasks and to learn some Ruby along the way. I've hit a bump now. How can I open a file for appending data to it on a certain line number.

Examlpe:
File:
jon
mike
me
you

I want to open the above file and add a name on line 4 (between "me" and "you") to get:

File:
jon
mike
me
NEWNAME
you

I know the exact line number I want to append data to, while pushing existing data down.

I was thinking of loading the file into an array line by line, then adding to the array and finally saving it back again, but this is ugly and I believe there is an easier way.

Thanks for help,

-NSKL
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP file writing benrose111488 Programming 6 07-01-2005 03:57 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
writing to file ej25 Programming 17 11-19-2004 11:23 AM
c++ writing to a .txt file xemous Programming 8 09-01-2004 10:57 PM
File writing error xylum Linux - General 2 09-08-2002 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration