Last active
December 16, 2015 03:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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