Help me with this code...
I want to read lines from log file, but apply filter to them.
I need not all lines, but only lines that contain given nick.
Logfile format is:
[2007_11_08 05:59] <user1>(10.27.42.206) hello
[2007_11_08 05:59] <user2>(10.179.99.250) hi
The command '+mlog 2007_11_08' works fine and read all lines.
And '+mlog 2007_11_08 user1' isn't work - again read all lines.
I think the error is in function loadtext, marked by --[[ ]] but I don't know how to fix.
The code follows:
function getFullLogs(sUser,arg)
local _,_,f, fdate,fnick = string.find(arg, "%b<>%s+%p(%S+)%s+(%S+)%s+(%S+)");
msg=""
logtype = F_MainLogPath..fdate.."_mainchat.log"
msg = msg..loadtext(logtype,fnick).."\r\n"
BotSayToUser(sUser,msg )
return 1
end
function loadtext(filename,nick)
local message = ""
local list = {}
local f = io.open( filename, "r" )
if f then
message=f:read("*all")
--[[
if nick ~= nil then
for line in f:lines() do
if string.find(line,nick) then table.insert(list,line); end
end
message=list
end
]]
f:close()
end
return message
end
try this
function loadtext(filename,nick)
local list = {}
local f = io.open( filename, "r" )
if f and nick then
for line in f:lines() do
if string.find(line,nick) then table.insert(list,line) end
end
f:close()
end
return list
end
Big thanks CG and Mutor, works fine! 8)
If it's 500% sure that the file exists, you ought to take io.lines() into consideration.
that code looks familiar, I wonder why... :P