Created
May 10, 2012 15:38
-
-
Save rusllonrails/2653969 to your computer and use it in GitHub Desktop.
Замена строк в файле виа reqexp
This file contains 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
namespace :db do | |
task :parse_lines => :environment do | |
new_file = File.open("#{Rails.root}/lib/tasks/dummy_2.rake", 'w+') | |
reqexp_list = [] | |
[/:id=>\d\d\d,/i, /:id=>\d\d,/i, /:id=>\d,/i].each do |reqexp| | |
reqexp_list << Regexp.new(reqexp) | |
end | |
File.open("#{Rails.root}/lib/tasks/dummy_1.rake", 'r+') do |file| | |
file.each_line do |line| | |
replace = false | |
reqexp_list.each do |reqexp| | |
if line.match(reqexp) | |
line = line.gsub(reqexp, '') | |
replace = true | |
end | |
end | |
new_file << line | |
end | |
new_file.close | |
file.close | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment