当前位置: 代码网 > it编程>前端脚本>Lua > lua日志文件处理代码

lua日志文件处理代码

2024年05月15日 Lua 我要评论
目前我找到的文件夹的搜索工具,最多可以完成把搜索到的单行的内容,进行输出出来,或者进行一些简单的处理,但是不够灵活。因此就用lua自己写了个,可以完成自己定义搜索处理函数,进行一些数据的处理,省去了将

目前我找到的文件夹的搜索工具,最多可以完成把搜索到的单行的内容,进行输出出来,或者进行一些简单的处理,但是不够灵活。

因此就用lua自己写了个,可以完成自己定义搜索处理函数,进行一些数据的处理,省去了将搜索结果放到excel中再处理的过程。

-- search_log.lua

tbresult = {};
tbcmdresult = {};

local sztmpfolderpath = os.getenv("temp");
if not sztmpfolderpath then
 os.execute("md c:\\temp")
 sztmpfolderpath = "c:\\temp";
end

local tbspecialworld =
{
 ["("] = "%(", [")"] = "%)", ["."] = "%.", ["%"] = "%%",
 ["+"] = "%+", ["-"] = "%-", ["*"] = "%*", ["?"] = "%?",
 ["["] = "%[", ["]"] = "%]", ["^"] = "%^", ["$"] = "%$",
};
function formatcmd(szcmd)
 return string.gsub(szcmd, ".", function(s) return tbspecialworld[s] or s; end)
end

function formatpath(szpath)
 string.gsub(szpath, "[\\/]$", "");
 return string.gsub(szpath, "/", "\\");
end

function checkfile(szfilepath)
 local file = io.open(szfilepath, "rb");
 if not file then
   return;
 end
 file:close();
 return true;
end

function openfile(szfilepath)
 if not checkfile(szfilepath) then
   return;
 end

 local tbfile = {};
 for line in io.lines(szfilepath) do
   table.insert(tbfile, line);
 end

 return tbfile;
end

function searchfile(szfilepath, szcmd, fncmd2line, fnfilename)
 local tbfile = openfile(szfilepath);
 if not tbfile then
   return;
 end

 tbresult[szfilepath] = tbresult[szfilepath] or {};
 local szcmdresult = "";
 for nline, szline in ipairs(tbfile) do
   if string.match(szline, szcmd) then
     szcmdresult = fncmd2line(szline);
     if szcmdresult and szcmdresult ~= "" then
       table.insert(tbcmdresult, szcmdresult);
     end
     table.insert(tbresult[szfilepath], nline .. ":" .. szline);
   end
 end

 return 1;
end

function cmd2line(szline)
 return;
end

function checkname(szfilename)
 return true;
end

function searchdir(szfolderpath, szcmd, fncmd2line, fncheckname, nidx)
 if not szcmd or szcmd == "" then
   return;
 end

 local fncmd2line = fncmd2line or cmd2line;
 local fncheckname = fncheckname or checkname;
 local nidx = nidx or 0;

 local sztmpfilename = sztmpfolderpath .. "\\searchdirtemp" .. nidx .. ".tmp";
 os.execute("dir /b ".. szfolderpath .." >" .. sztmpfilename);

 local tbfile = openfile(sztmpfilename);
 if not tbfile or #tbfile == 0 then
   return;
 end

 local szpath = "";
 for _, szfilename in ipairs(tbfile) do
   szpath = szfolderpath .. "\\" .. szfilename;
   if not checkfile(szpath) then
     searchdir(szpath, szcmd, fncmd2line, nidx + 1);
    else
      if checkname(szfilename) then
        searchfile(szpath, szcmd, fncmd2line);
      end
    end
  end
end

function write2file(szinfo, szfilepath)
  local file = io.open(szfilepath, "w");
  if not file then
    print(szinfo);
    print("write2file err ?? not file " .. szfilepath);
    return;
  end

  file:write(szinfo);
  file:close();
end

function dosearchdir(szfolderpath, szcmd, tbparam)
  if not szfolderpath or szfolderpath == "" or not szcmd or szcmd == "" then
    return;
  end
 
  tbparam = tbparam or {};

  szfolderpath = formatpath(szfolderpath);
  if tbparam.bismatch then
    szcmd = formatcmd(szcmd);
  end
  local ntime = os.time();
  searchdir(szfolderpath, szcmd, tbparam.fncmd2line or cmd2line, tbparam.fncheckname or checkname, 0);
  ntime = os.time() - ntime;
  print("搜索用时:" .. ntime);

  local szresultpath = tbparam.szresultpath or (sztmpfolderpath .. "\\result.tab.tmp");
  local szresult = "";
  for szfilepath, tbinfo in pairs(tbresult) do
    szresult = szresult .. szfilepath .. "\n";
    for _, szline in pairs(tbinfo) do
      szresult = szresult .. szline .. "\n";
    end
  end
  write2file(szresult, szresultpath);

  local szcmdresult = "";
  for _, szline in pairs(tbcmdresult) do
    szcmdresult = szcmdresult .. szline .. "\n";
  end
  write2file(szcmdresult, tbparam.szcmdresultpath or (sztmpfolderpath .. "\\cmd_result.tab.tmp"));
end

--tbparam =
--{
--  bismatch = false;  -- 是否使用正则方式搜索
--  fncmd2line = function () end; -- 自定义搜索行内容处理函数
--  fncheckname = function () end; -- 文件名限定函数
--  szresultpath = "e:\\result.tab"; -- 文件搜索内容输出路径
--  szcmdresultpath = "e:\\cmd_result.tab"; -- 自定义处理函数返回内容储存路径
--}

使用代码可以如下(貌似支持网络路径的):

dofile("e:\\search_log.lua");
tbtmpinfo = {};
function checkinfo(szline)
 local szplayername, nplayerid, ncount = string.match(szline, "^.*sztype = final\t[^\t]+\t%d+\t([^\t]+)\t(%d+)\t(%d+).*$");
 nplayerid = tonumber(nplayerid);
 ncount = tonumber(ncount);
 if ncount > tbtmpinfo[nplayerid] then
   tbtmpinfo[nplayerid] = ncount;
    return "" .. nplayerid .. "\t" .. ncount;
  end
  return;
end
tbparam =
{
  bismatch = false;
  fncmd2line = checkinfo;
  fncheckname = function () return true; end;
  szresultpath = "e:\\result.tab";
  szcmdresultpath = "e:\\cmd_result.tab";
}
dosearchdir("d:\\logs", "sztype = final", tbparam);
for _, szinfo in pairs(tbtmpinfo) do
  print(szinfo);
end

唯一不满意的地方貌似是搜索速度有点慢,以后有空再调整吧,现在这个暂时够用了,至少比原来方便多了~~

(0)

相关文章:

  • Lua中string.lower()使用指南

    Lua中string.lower()使用指南

    前言今天我们总结的函数也比较简单,函数的作用的将所给字符串的中的大写字母转换成小写字母,这种操作往往出现在比较操作之前,比如验证码通常都是不区分大小写的,接下来... [阅读全文]
  • linux系统安装Nginx Lua环境

    linux系统安装Nginx Lua环境

    亦可参考官方安装指南: lua-nginx-module installation这是我总结的安装,供参考:需要最新版的nginx,luajit,ngx_dev... [阅读全文]
  • Nginx+lua 实现调用.so文件

    Nginx+lua 实现调用.so文件

    最近在和智能硬件部门一起,做一个室内定位的服务,该服务根据手机端传过来的beacon设备列表,根据一定的算法计算出具体的商场,并将商场id和beason设备列表... [阅读全文]
  • Lua中设置table为只读属性的方法详解

    Lua中设置table为只读属性的方法详解

    项目中部分只读表易被人误改写,故决定在非线上环境里对这些表附加只读属性,方便在出现误改写的时候抛出lua错误,最终版代码如下:--[[-------------... [阅读全文]
  • Lua Table转C# Dictionary的方法示例

    Lua Table转C# Dictionary的方法示例

    table特性table是一个“关联数组”,数组的索引可以是数字或者是字符串,所有索引值都需要用 "["和"]" 括起来;如果是字符串,还可以去掉引号和中括号;... [阅读全文]
  • Lua语言新手简单入门教程

    Lua语言新手简单入门教程

    一、前言lua 是一种轻量小巧的脚本语言,用标准 c 语言编写并以源代码形式开放,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。lua... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com