Hello everyone,
Take 1 xls file witch contains 2 columns. column A contains several
“keywords” lets say: today, today+1, today+2 etc. and column B witch
contains dates in yyyymmdd format 20090706, 20090707, 20090708 etc.
i have another file (txt) witch contains a bunch of text… including our
“keywords”
What i need to do is… search in the txt file for the “keywords” and
replace them with the dates and write the new content to a different
file (same name and extension) in a different folder.
The code is as follows:
require ‘rubygems’
require ‘roo’
oo=Excel.new(“data.xls”)
1.upto(56000) do |line|
template = oo.cell(line,‘A’)
date = oo.cell(line,‘B’)
date = date.to_s.gsub!(’-’,’’)
date_template = { /\b#{template}\b(?=\s|$)/ => “#{date}”}
Dir[’*.txt’].each do |file_name|
replace = File.read( file_name )
if template != nil
date_template.each do |template, date|
replace.gsub!( template, date )
end
puts replace
end
File.makedirs(“results”)
File::open(“results/#{file_name}”,“w”) do |file_name|
file_name<<replace
end
end
end
Problems I have:
#1 The script enters a loop, freezes after a number of loops, then stops
with exit code: 0
#2 ‘puts replace’ shows me that the first loop replaces the first
“keyword” it finds, the next ones (10-20) don’t replace anything then it
stars replacing again
#3 the file created is the same as the original (no replace was made)
#4 i don’t really know if the regex above is correct. I used it to do a
“whole word matching” and it seems to be ok for now.
#5 problems I have yet do discover
I know the code is a mess but bare with me, I’ve been studying ruby only
for a month or so.
Any tips & tricks will be greatly appreciated
I have attached the txt file witch needs to be read and replaced.
Thank you.
AC