need help with reading/writing to files

B

Ben

I've been trying to write a program that will take a file, do some
find/replace operation and save the file. I'm pretty new to ruby, and I
can't seem to figure out how to do this succinctly. This is my latest
attempt, but it seems like I shouldn't need to open the file twice.

I tried to just open the file with "r+" permissions, but if I overwrote
it with a string that was shorter than the original contents, the extra
characters would stick around. I couldn't figure out how to slice off
the extra stuff I didn't need.

Any suggestions on how to reduce this to one call to File.open???
Thanks! Any other suggestions are also appreciated. Here's the code...

print "pattern:"
pattern = STDIN.gets.chop
print "replacement:"
replacement = STDIN.gets.chop
str = ""
ARGV.each do |file|
File.open(file,"r+") do |handle|
str = handle.read
end
File.open(file,"w+") do |handle|
str.gsub!(/#{pattern}/,replacement)
handle.write(str)
end
end



Thanks,

Ben
 
M

Mark Hubbart

I've been trying to write a program that will take a file, do some
find/replace operation and save the file. I'm pretty new to ruby, and I
can't seem to figure out how to do this succinctly. This is my latest
attempt, but it seems like I shouldn't need to open the file twice.

I tried to just open the file with "r+" permissions, but if I overwrote
it with a string that was shorter than the original contents, the extra
characters would stick around. I couldn't figure out how to slice off
the extra stuff I didn't need.

Any suggestions on how to reduce this to one call to File.open???
Thanks! Any other suggestions are also appreciated. Here's the code...

print "pattern:"
pattern = STDIN.gets.chop
print "replacement:"
replacement = STDIN.gets.chop
str = ""
ARGV.each do |file|
File.open(file,"r+") do |handle|
str = handle.read
end
File.open(file,"w+") do |handle|
str.gsub!(/#{pattern}/,replacement)
handle.write(str) handle.truncate(handle.pos)
end
end

File#truncate(size) truncates the file to the given size. Passing your
current position after you write to the file truncates it immediately
after the last character you wrote.

HTH,
Mark
 
G

Glenn Parker

Ben said:
I've been trying to write a program that will take a file, do some
find/replace operation and save the file. I'm pretty new to ruby, and I
can't seem to figure out how to do this succinctly. This is my latest
attempt, but it seems like I shouldn't need to open the file twice.

Have you considered doing it all from the command line?

% ruby -pi -e 'gsub /pattern/, "replacement"' file1 file2...
 
B

Ben

File#truncate was exactly what I was looking for. Thanks! Also, thanks
for the suggestion about the command line, I might try that as well,
but I might be making the script a little more complicated so it might
be a bit much for the command line. Thanks, both! I appreciate it.

Ben
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top