changing a file inline on Windows

J

James Dinkel

Here is my current method for editing files:

<code>

filename = File.join('path', 'to', 'file')
content = []
File.open(filename, 'r') do |file|
content = file.readlines
end

content.collect! do |line|
line.gsub!(/six/, "half a dozen")
line.chomp
end

File.open(filename, 'w') do |file|
file.write content.join($/)
end

</code>

This is expensive though, as it rewrites the entire file, even if only a
single word in the whole file is changed. I'm wondering if there is a
better way to do this.

James
 
R

Robert Klemme

Here is my current method for editing files:

<code>

filename = File.join('path', 'to', 'file')
content = []
File.open(filename, 'r') do |file|
content = file.readlines
end

content.collect! do |line|
line.gsub!(/six/, "half a dozen")
line.chomp
end

File.open(filename, 'w') do |file|
file.write content.join($/)

This is a killer because it will transform the whole file into a single
String just for printing. You better do either of this

1. just do "file.puts content"

2. work with String the whole way, i.e.

content = File.read file_name
content.gsub! /six/, "half a dozen"
File.open(file_name, "w") {|io| io.write content}

First alternative has the advantage of being a smaller change to your
code and also I believe it's nicer to the GC; second solution has the
advantage that you can do multiline replacements.
end

</code>

This is expensive though, as it rewrites the entire file, even if only a
single word in the whole file is changed. I'm wondering if there is a
better way to do this.

For text files the situation is usually this: the replacement has
different length than the original. Thus, you need to at least rewrite
all the stuff from the original string's starting position on. The code
becomes a bit more complex and if the file is small it's probably not
worthwhile.

Note also that there is command line option -i which will allow for easy
inline edits:

ruby -i.bak -pe 'gsub /six/, "half a dozen"' your_file

You can also use it in scripts:

#!/bin/ruby -i.bak -p
gsub /six/, "half a dozen"

Kind regards

robert
 
L

lrlebron

You can use the rio library to simplify this

require 'rio'
rio(f) <f.contents.gsub(/six/,"half a dozen")
 
L

lrlebron

Here is my current method for editing files:

<code>

  filename = File.join('path', 'to', 'file')
  content = []
  File.open(filename, 'r') do |file|
    content = file.readlines
  end

  content.collect! do |line|
    line.gsub!(/six/, "half a dozen")
    line.chomp
  end

  File.open(filename, 'w') do |file|
    file.write content.join($/)
  end

</code>

This is expensive though, as it rewrites the entire file, even if only a
single word in the whole file is changed.  I'm wondering if there is a
better way to do this.

James

There was a small error in my previous post. Here a more complete
script

require 'rio'
ario = rio(filename)
ario < ario.contents.gsub(/six/, "half a dozen")
 
J

James Dinkel

Thanks for all the information. I'll try out these methods and try to
get an idea which one I like best.

James
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top