I have a file with rows of an SQL dump. It has dates in the format yyyy-mm-dd
(eg. 2012-08-13
)
我有一个包含SQL转储行的文件。它的格式为yyyy-mm-dd格式(例如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-13
I want to change to be stored as something like$$MAINDATE$$
-
2012-08-14
I want to be$$MAINDATE+1$$
2012-08-13我想改变以存储为类似$$ MAINDATE $$的东西
2012-08-14我想成为$$ 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.
然后我想迭代文件并用基于当时参数的新日期替换它们。并根据+1或+100调整日期,或者在最后几天之后调整日期。
I think the match text is /\d{4}-\d{2}-\d{2}/
我认为匹配文字为/ \ 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
2 个解决方案
#1
1
require "date"
def format_log(str, marker, refdate)
rules = Hash[str.scan(/\d{4}-\d{2}-\d{2}/m).uniq.collect{|e| [e, (Date.parse(e)-refdate).to_i]}]
rules.each do |key, value|
replacement = "%s%s" % [value >= 0 ? "+" : "", value]
str.gsub!(key, marker % replacement )
end
str
end
p format_log("2012-08-13\n2012-08-14\n2012-08-12", "$$MAINDATE%s$$", Date.today)
will output
"$$MAINDATE+0$$\n$$MAINDATE+1$$\n$$MAINDATE-1$$"
#2
2
Something like this:
像这样的东西:
require 'date'
def munge_dates list, base_date = Date.today
date_regexp = /\d{4}-\d{2}-\d{2}/
list.map do |line|
days_difference = "%+d" % (Date.parse(line[date_regexp]) - base_date)
line.sub date_regexp,"$$MAINDATE#{days_difference}$$"
end
end
If you don't want a $$MAINDATE+0$$ in there then some conditional logic would need to be added.
如果你不想在那里使用$$ MAINDATE + 0 $$,那么需要添加一些条件逻辑。
#1
1
require "date"
def format_log(str, marker, refdate)
rules = Hash[str.scan(/\d{4}-\d{2}-\d{2}/m).uniq.collect{|e| [e, (Date.parse(e)-refdate).to_i]}]
rules.each do |key, value|
replacement = "%s%s" % [value >= 0 ? "+" : "", value]
str.gsub!(key, marker % replacement )
end
str
end
p format_log("2012-08-13\n2012-08-14\n2012-08-12", "$$MAINDATE%s$$", Date.today)
will output
"$$MAINDATE+0$$\n$$MAINDATE+1$$\n$$MAINDATE-1$$"
#2
2
Something like this:
像这样的东西:
require 'date'
def munge_dates list, base_date = Date.today
date_regexp = /\d{4}-\d{2}-\d{2}/
list.map do |line|
days_difference = "%+d" % (Date.parse(line[date_regexp]) - base_date)
line.sub date_regexp,"$$MAINDATE#{days_difference}$$"
end
end
If you don't want a $$MAINDATE+0$$ in there then some conditional logic would need to be added.
如果你不想在那里使用$$ MAINDATE + 0 $$,那么需要添加一些条件逻辑。