[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

insert into textfile

Ron Green

9/14/2008 6:42:00 AM

I have a text file that I want to insert a new line into between the
first and second line. Is there a way to do this?

2 Answers

thomas peklak

9/14/2008 12:37:00 PM

0

content = File.read('test.txt')
content.sub!("\n","\n\n")
File.open('test.txt','w').write(content)

sub does replace the first occurence of a new line in the file

thomas

Ron Green

9/14/2008 8:32:00 PM

0

Actually, that was close.
This is what I was kind of looking for.

content =3D File.read('exam.txt')
content.sub!("\n","\nNew Content\n")
File.open('exam.txt','r+').write(content)

But this doesn't work if the first line doesn't end in a new line
character.

On Sep 14, 7:33=A0am, thomas peklak <thomas.pek...@gmail.com> wrote:
> content =3D File.read('test.txt')
> content.sub!("\n","\n\n")
> File.open('test.txt','w').write(content)
>
> sub does replace the first occurence of a new line in the file
>
> thomas