lua日志文件处理代码
目前我找到的文件夹的搜索工具,最多可以完成把搜索到的单行的内容,进行输出出来,或者进行一些简单的处理,但是不够灵活。
因此就用lua自己写了个,可以完成自己定义搜索处理函数,进行一些数据的处理,省去了将搜索结果放到excel中再处理的过程。
--search_log.lua
tbResult={};
tbCmdResult={};
localszTmpFolderPath=os.getenv("temp");
ifnotszTmpFolderPaththen
os.execute("mdc:\\temp")
szTmpFolderPath="c:\\temp";
end
localtbSpecialWorld=
{
["("]="%(",[")"]="%)",["."]="%.",["%"]="%%",
["+"]="%+",["-"]="%-",["*"]="%*",["?"]="%?",
["["]="%[",["]"]="%]",["^"]="%^",["$"]="%$",
};
functionFormatCmd(szCmd)
returnstring.gsub(szCmd,".",function(s)returntbSpecialWorld[s]ors;end)
end
functionFormatPath(szPath)
string.gsub(szPath,"[\\/]$","");
returnstring.gsub(szPath,"/","\\");
end
functionCheckFile(szFilePath)
localfile=io.open(szFilePath,"rb");
ifnotfilethen
return;
end
file:close();
returntrue;
end
functionOpenFile(szFilePath)
ifnotCheckFile(szFilePath)then
return;
end
localtbFile={};
forlineinio.lines(szFilePath)do
table.insert(tbFile,line);
end
returntbFile;
end
functionSearchFile(szFilePath,szCmd,fnCmd2Line,fnFileName)
localtbFile=OpenFile(szFilePath);
ifnottbFilethen
return;
end
tbResult[szFilePath]=tbResult[szFilePath]or{};
localszCmdResult="";
fornLine,szLineinipairs(tbFile)do
ifstring.match(szLine,szCmd)then
szCmdResult=fnCmd2Line(szLine);
ifszCmdResultandszCmdResult~=""then
table.insert(tbCmdResult,szCmdResult);
end
table.insert(tbResult[szFilePath],nLine..":"..szLine);
end
end
return1;
end
functionCmd2Line(szLine)
return;
end
functionCheckName(szFileName)
returntrue;
end
functionSearchDir(szFolderPath,szCmd,fnCmd2Line,fnCheckName,nIdx)
ifnotszCmdorszCmd==""then
return;
end
localfnCmd2Line=fnCmd2LineorCmd2Line;
localfnCheckName=fnCheckNameorCheckName;
localnIdx=nIdxor0;
localszTmpFileName=szTmpFolderPath.."\\SearchDirTemp"..nIdx..".tmp";
os.execute("dir/b"..szFolderPath..">"..szTmpFileName);
localtbFile=OpenFile(szTmpFileName);
ifnottbFileor#tbFile==0then
return;
end
localszPath="";
for_,szFileNameinipairs(tbFile)do
szPath=szFolderPath.."\\"..szFileName;
ifnotCheckFile(szPath)then
SearchDir(szPath,szCmd,fnCmd2Line,nIdx+1);
else
ifCheckName(szFileName)then
SearchFile(szPath,szCmd,fnCmd2Line);
end
end
end
end
functionWrite2File(szInfo,szFilePath)
localfile=io.open(szFilePath,"w");
ifnotfilethen
print(szInfo);
print("Write2FileERR??notfile"..szFilePath);
return;
end
file:write(szInfo);
file:close();
end
functionDoSearchDir(szFolderPath,szCmd,tbParam)
ifnotszFolderPathorszFolderPath==""ornotszCmdorszCmd==""then
return;
end
tbParam=tbParamor{};
szFolderPath=FormatPath(szFolderPath);
iftbParam.bIsMatchthen
szCmd=FormatCmd(szCmd);
end
localnTime=os.time();
SearchDir(szFolderPath,szCmd,tbParam.fnCmd2LineorCmd2Line,tbParam.fnCheckNameorCheckName,0);
nTime=os.time()-nTime;
print("搜索用时:"..nTime);
localszResultPath=tbParam.szResultPathor(szTmpFolderPath.."\\result.tab.tmp");
localszResult="";
forszFilePath,tbInfoinpairs(tbResult)do
szResult=szResult..szFilePath.."\n";
for_,szLineinpairs(tbInfo)do
szResult=szResult..szLine.."\n";
end
end
Write2File(szResult,szResultPath);
localszCmdResult="";
for_,szLineinpairs(tbCmdResult)do
szCmdResult=szCmdResult..szLine.."\n";
end
Write2File(szCmdResult,tbParam.szCmdResultPathor(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={};
functionCheckInfo(szLine)
localszPlayerName,nPlayerId,nCount=string.match(szLine,"^.*szType=final\t[^\t]+\t%d+\t([^\t]+)\t(%d+)\t(%d+).*$");
nPlayerId=tonumber(nPlayerId);
nCount=tonumber(nCount);
ifnCount>tbTmpInfo[nPlayerId]then
tbTmpInfo[nPlayerId]=nCount;
return""..nPlayerId.."\t"..nCount;
end
return;
end
tbParam=
{
bIsMatch=false;
fnCmd2Line=CheckInfo;
fnCheckName=function()returntrue;end;
szResultPath="e:\\result.tab";
szCmdResultPath="e:\\cmd_result.tab";
}
DoSearchDir("d:\\logs","szType=final",tbParam);
for_,szInfoinpairs(tbTmpInfo)do
print(szInfo);
end
唯一不满意的地方貌似是搜索速度有点慢,以后有空再调整吧,现在这个暂时够用了,至少比原来方便多了~~