Skip to content

Instantly share code, notes, and snippets.

@chenzx
Last active December 16, 2015 03:29
Show Gist options
  • Select an option

  • Save chenzx/5369810 to your computer and use it in GitHub Desktop.

Select an option

Save chenzx/5369810 to your computer and use it in GitHub Desktop.
Do plain string replace (use string#gsub API) of a collection of files
#!/usr/bin/env ruby
# encoding: utf-8
#
#Usage: ruby files_content_replace.rb <filePathLists.txt>
File.open(ARGV[0]).each { |filePath|
correctedFileContentLines = []
f = File.open(filePath.strip)
f.each { |line|
line2 = line.gsub("old text", "new text")
if line2!=line then
puts "#{line} => #{line2}"
end
correctedFileContentLines.push line2
}
f.close #Must close it before write new content;
File.new(filePath.strip, "w").write( correctedFileContentLines.join )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment