Sunday, October 26, 2014

Ruby Sample Codes to Replace String in Files

I collected sample Ruby codes for replacing string in file.

1. http://stackoverflow.com/questions/1274605/ruby-search-file-text-for-a-pattern-and-replace-it-with-a-given-value

file_names = ['foo.txt', 'bar.txt']

file_names.each do |file_name|
  text = File.read(file_name)
  new_contents = text.gsub(/search_regexp/, "replacement string")

  # To merely print the contents of the file, use:
  puts new_contents

  # To write changes to the file, use:
  File.open(file_name, "w") {|file| file.puts new_contents }
end

2. http://stackoverflow.com/questions/7290871/ruby-how-to-replace-text-in-a-file

replace="100"
filename = "xmlfile_in"
outdata = File.read(filename).gsub(/\d+<\/appId>/, "#{replace}")

File.open(filename, 'w') do |out|
  out << outdata
end

3. http://stackoverflow.com/questions/21762209/how-to-search-and-replace-strings-in-all-of-the-files-in-my-directory-using-ruby

# Set the variables for find/replace
# you can use regular variables here
original_string_or_regex = /Card Sort/
replacement_string = "CardSort"

# Dir.glob will take care of the recursivity for you
# do not use ~ but rather Dir.home
Dir.glob("#{Dir.home}/Desktop/macshapa_v2/*") do |file_name|
  text = File.read(file_name)
  replace = text.gsub!(original_string_or_regex, replacement_string)
  File.open(file_name, "w") { |file| file.puts replace }
end

4. http://www.java2s.com/Code/Ruby/File-Directory/Replacestringinafile.htm

def stringReplace(searchString, replaceString, fileName)
  aFile = File.open(fileName, "r")
  aString = aFile.read
  aFile.close

  aString.gsub!(searchString, replaceString)

  File.open(fileName, "w") { |file| file << aString }
end

stringReplace(*ARGV)

5. http://stackoverflow.com/questions/8063121/how-to-search-and-replace-string-in-ruby

text = File.read("reporttestphp2.xml").gsub("]]>*/-->","*/-->")
File.open("out.xml", "w").write(text)

6. http://blog.hulihanapplications.com/browse/view/30-recursive-text-replacement-with-ruby-linux

#!/usr/bin/env ruby  
  
require "eregex"  
  
# Recursive String Replacement, starting in current dir.  
# Author: Dave Hulihan  
  
if ARGV.length < 2  
        puts "Usage: recursive_replace [string1] [string2]"  
        exit 0  
end  
  
@string1 = ARGV[0]  
@string2 = ARGV[1]  
  
puts "Replacing #{@string1} with #{@string2}..."  
  
@string1 = Regexp.escape(@string1) # escape any special characters  
@string2 = Regexp.escape(@string2)  
  
exec("find ./ -type f | xargs sed -i 's/#{@string1}/#{@string2}/g'")  
  
puts "Done!"  
- See more at: http://blog.hulihanapplications.com/browse/view/30-recursive-text-replacement-with-ruby-linux#sthash.ez8BMRmJ.dpuf

7. http://archive.railsforum.com/viewtopic.php?id=28378

path = '/home/sthapit/workspace/socialect'
extensions = %w(.rb .css .erb .builder .rjs .js)
    
require 'find'
Find.find(path) do |f|
  if extensions.include? File.extname(f)
    buffer = File.new(f,'r').read.gsub(/slog/,'post').gsub(/Slog/,'Post')
    File.open(f,'w') {|fw| fw.write(buffer)}
  end
end

No comments:

Post a Comment