Add some content to a file

Hello,

I want to modify a file, begin from X line (X is line number),
inserting some content into it.
I tried scripting it with ruby but couldn't find a correct way.
Can you help with it? Thanks.

Jenn.

Inserting is hard - because you have to move all the content behind that line to the end of the file. It's easier if you just read the original file and write it to another file. Read X - 1 lines from source file and write them to temp file. Then write the new content. Then write the remainder of the file.

If you want to do the insertion with a single file only you need to determine the file offset of line X and then start moving blocks of data starting from the end, e.g. read 4k from the end, seek to pos + inserted.length, write the 4k. Move back to the second but last 4k, read them seek to pos + inserted.length, write them etc. This is more complicated to write and also likely more inefficient IO wise.

Kind regards

  robert

ยทยทยท

On 02/05/2010 11:43 AM, Ruby Newbee wrote:

I want to modify a file, begin from X line (X is line number),
inserting some content into it.
I tried scripting it with ruby but couldn't find a correct way.
Can you help with it? Thanks.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/