Author Topic: read lines  (Read 2402 times)

0 Members and 1 Guest are viewing this topic.

Offline sphinx_spb

  • Member
  • ***
  • Posts: 47
  • Karma: +10/-3
read lines
« on: 08 November, 2007, 20:09:52 »
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:

Code: [Select]
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

PtokaX forum

read lines
« on: 08 November, 2007, 20:09:52 »

Offline CrazyGuy

  • Viking
  • ****
  • Posts: 506
  • Karma: +83/-20
    • ?????=-_The NightHawk_-=?????
Re: read lines
« Reply #1 on: 09 November, 2007, 00:38:16 »
try this

Code: [Select]
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

Offline sphinx_spb

  • Member
  • ***
  • Posts: 47
  • Karma: +10/-3
Re: read lines
« Reply #2 on: 09 November, 2007, 18:11:22 »
Big thanks CG and American Idiot, works fine! 8)

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 725
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: read lines
« Reply #3 on: 09 November, 2007, 20:12:05 »
If it's 500% sure that the file exists, you ought to take io.lines() into consideration.
Everything could have been anything else and it would have just as much meaning.

Offline CrazyGuy

  • Viking
  • ****
  • Posts: 506
  • Karma: +83/-20
    • ?????=-_The NightHawk_-=?????
Re: read lines
« Reply #4 on: 10 November, 2007, 15:21:42 »
that code looks familiar, I wonder why...  :P

PtokaX forum

Re: read lines
« Reply #4 on: 10 November, 2007, 15:21:42 »