lua-users home
lua-l archive

Re: Using Lua to combine CSV files

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The following is a straightforward solution (not optimized for
performance).
local timestamp1, timestamp2, first, last, outlogfile = ...
timestamp1, timestamp2 = tonumber(timestamp1), tonumber(timestamp2)
local fout = assert (io.open (outlogfile, "w"))
local done
for i = first, last do
 if done then break end
 local filename = "logfile" .. i .. ".csv"
 for line in io.lines(filename) do
 local timestamp = tonumber (line:match ("^[^,]+"))
 if timestamp then
 if timestamp >= timestamp1 then
 if timestamp <= timestamp2 then fout:write (line, "\n")
 else done = true break
 end
 end
 else
 error ("record with bad timestamp in file " .. filename)
 end
 end
end
fout:close()
--
Shmuel

AltStyle によって変換されたページ (->オリジナル) /