开发者

Why won't gsub! change my files?

开发者 https://www.devze.com 2023-03-24 13:38 出处:网络
I am trying to do a simple find/replace on all text files in a directory, modifying any instance of [RAVEN_START: by inserting a string (in this case \'raven was here\') before the line.

I am trying to do a simple find/replace on all text files in a directory, modifying any instance of [RAVEN_START: by inserting a string (in this case 'raven was here') before the line.

Here is the entire ruby program:

#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils' #for FileUtils.mv('your file', 'new location')

class RavenParser

  rawDir = Dir.glob("*.txt")
  count = 0
  rawDir.each do |ravFile|
    #we have selected every text file, so now we have to search through the file
    #and make the needed changes.
    rav = File.open(ravFile, "r+") do |modRav|
      #Now we've opened the file, and we need to do the operations.
        if modRav            
        lines = File.open(modRav).readlines
        lines.each { |line|
        if line.match /\[RAVEN_START:.*\]/ 
          line.gsub!(/\[RAVEN_START:/, 'raven was here '+line)
          count = count + 1
        end
        }
         printf("Total Changed: %d\n",count)
      else
         printf("No txt files found. \n")
      end
    end
    #end of file replacing instructions.
  end

  # S
end开发者_开发问答

The program runs and compiles fine, but when I open up the text file, there has been no change to any of the text within the file. count increments properly (that is, it is equal to the number of instances of [RAVEN_START: across all the files), but the actual substitution is failing to take place (or at least not saving the changes).

Is my syntax on the gsub! incorrect? Am I doing something else wrong?


You're reading the data, updating it, and then neglecting to write it back to the file. You need something like:

# And save the modified lines.
File.open(modRav, 'w') { |f| f.puts lines.join("\n") }

immediately before or after this:

printf("Total Changed: %d\n",count)

As DMG notes below, just overwriting the file isn't properly paranoid as you could be interrupted in the middle of the write and lose data. If you want to be paranoid (which all of us should be because they really are out to get us), then you want to write to a temporary file and then do an atomic rename to replace the original file the new one. A rename generally only works when you stay within a single file system as there is no guarantee that the OS's temp directory (which Tempfile uses by default) will be on the same file system as modRav so File.rename might not even be an option with a Tempfile unless precautions are taken. But the Tempfile constructor takes a tmpdir parameter so we're saved:

modRavDir = File.dirname(File.realpath(modRav))
tmp       = Tempfile.new(modRav, modRavDir)
tmp.write(lines.join("\n"))
tmp.close
File.rename(tmp.path, modRav)

You might want to stick that in a separate method (safe_save(modRav, lines) perhaps) to avoid further cluttering your block.


  1. There is no gsub! in the post (except the title and question). I would actually recommend not using gsub!, but rather use the result of gsub -- avoiding mutability can help reduce a number of subtle bugs.

  2. The line read from the file stream into a String is a copy and modifying it will not affect the contents of the file. (The general approach is to read a line, process the line, and write the line. Or do it all at once: read all lines, process all lines, write all processed lines. In either case, nothing is being written back to the file in the code in the post ;-)

Happy coding.


You're not using gsub!, you're using gsub. gsub! and gsub different methods, one does replacement on the object itself and the other does replacement then returns the result, respectively.

Change this

line.gsub(/\[RAVEN_START:/, 'raven was here '+line)

to this :

line.gsub!(/\[RAVEN_START:/, 'raven was here '+line)

or this:

line = line.gsub(/\[RAVEN_START:/, 'raven was here '+line)

See String#gsub for more info

0

精彩评论

暂无评论...
验证码 换一张
取 消