From: Garthy D Date: 2012-04-16T16:56:01+09:00 Subject: Re: insert text after line "whatever" Hi fox, On 16/04/12 15:57, fox foxmaster wrote: > Hi all, > > I have a problem that I am struggling with. > > Problem I have is that I want to insert text in a text file after a > specific line. > > My thought was to copy the original file line by line and when I got to > a specific line it would insert the text that I wanted This sounds like the approach that I would use. Open a source file to read, open the destination file. Read it in a line at a time, write it out. Make changes as you go, adding lines, removing them, etc. At EOF, close both files. Remove the old source and replace with the destination if desired. A Variation includes holding the whole new file in memory and writing it out in one step at the end. >, but I did not > manage to do this Do you have your attempt at this to share? Someone might be able to spot the problem if you post it. > so I went another way, like this > > File.open('test.bak', 'r+') do |file| > file.each do |line| > if line =~ /\<\/DHCPServer>/ #trigger > file.seek(-line.size, IO::SEEK_CUR) > file.puts ("Hello") #text to input > end > end > end > > But when I use this, first it manipulates the original file and it > deletes some of the letter of the trigger "" > > So can someone guide me to the right path? > > Regards > Fox > Garth