Add new text to files

C

Chris Conley

Hello,

I'm trying to insert new text into the middle of a file with the
following:

file = File.open("file.rb", "r+")
file.each { |line|
if line.match(/line text/)
file.puts("hello")
end
}

This works fine except that it overwrites the existing 5 characters to
replace with "hello". Is there a way to insert a new line with new
text without overwriting any existing data?

Thanks!
Chris
 
S

Simon Blanco

Chris said:
Hello,

I'm trying to insert new text into the middle of a file with the
following:

file = File.open("file.rb", "r+")
file.each { |line|
if line.match(/line text/)
file.puts("hello")
end
}

This works fine except that it overwrites the existing 5 characters to
replace with "hello". Is there a way to insert a new line with new
text without overwriting any existing data?

Thanks!
Chris

try changing:
file = File.open("file.rb", "r+")

for:
file = File.open("file.rb", "a+")


that should do it!
 
R

Rob Biedenharn

try changing:
file = File.open("file.rb", "r+")

for:
file = File.open("file.rb", "a+")

that should do it!


Uh, no, that would just cause the writes to go the end of the file.

You have to open up space in the file to do this. The easiest way is
to copy lines from the original file and put the possibly altered
lines into a new file. If you want, you can rename the files when
you're done (and delete the original).

You might also want to look at the ruby command line flag '-i' and see
if that gives you some ideas.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
L

luka luka

Chris said:
Hello,

I'm trying to insert new text into the middle of a file with the
following:

file = File.open("file.rb", "r+")
file.each { |line|
if line.match(/line text/)
file.puts("hello")
end
}

This works fine except that it overwrites the existing 5 characters to
replace with "hello". Is there a way to insert a new line with new
text without overwriting any existing data?

Hello Chris :)
Try count lines, and when line = (what do you want, exmaple 10),
continue puts into the file.
 
B

Bryan JJ Buckley

Hi,
To edit a file in place, you could try something like

File.open('file.rb', 'r+') do |file|
file.each do |line|
if line =~ /line text/
file.seek(-line.size, IO::SEEK_CUR)
file.puts("Hello")
end
end
end

Note the minus before line.size. This will probably fall over on
non-ascii files, as you're seeking back too little (String#size
returns the number of letters).

However, this is a C-ish approach, and unless you want your programme
to run on a tamagotchi, or something, you're probably better off just
reading in one file, and writing to another.
 
C

Chris Conley

[Note: parts of this message were removed to make it a legal post.]

Hi all, thanks for the info!

I ended up finding this solution from digging through some Rails generators:

def gsub_file(path, regexp, *args, &block)
content = File.read(path).gsub(regexp, *args, &block)
File.open(path, 'wb') { |file| file.write(content) }
end

line = 'Rails::Initializer.run do |config|'
gsub_file 'config/environment.rb', /(#{Regexp.escape(line)})/mi do |match|
"#{match}\n hello\n"
end

It basically reads the file, finds and replaces the line with itself and
whatever you want to add in to 'content'. Then writes back to the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top