I have a file with rows of an SQL dump. It has dates in the format yyyy-mm-dd (eg. 2012-08-13)
I’d like to replace all those dates with a marker so that in the future I can populate the database with rows that are all centered around the date at the time.
For example
2012-08-13I want to change to be stored as something like$$MAINDATE$$2012-08-14I want to be$$MAINDATE+1$$
This way they are all stored relative to a single date and the data will make sense backwards and forwards off the new creation date.
Then I’d like to iterate the file and replace them all with a new date based on an argument at the time. And adjusted dates based on the +1 or +100 or however many days away it will be at the end.
I think the match text is /\d{4}-\d{2}-\d{2}/
But how do I replace the text that is matched with a new term taking up and replacing the old text?
Update:
I used this to change the markers back to dates.. I doubt its pretty but its working
#iterate each line and look for the marker
while (line = infile.gets)
str = line
#replace marker with data modified by the modifier
rules = Hash[str.scan(/(\$\$MAINDATE(\+|\-)\d{1,5}\$\$)/).uniq.collect do |e|
modifyValue = e[0].split(e[1])[1].gsub("$","").to_i
if e[1] == "-" then
modifyValue = modifyValue * -1
end
[e[0], (today + modifyValue).to_s]
end ]
rules.each do |key, value|
str.gsub!(key, value)
end
#write new line to array
finishedText.push str
end
will output