需求
有一个文本文件,里面有大量的重复行,要求把重复的部分去掉.我本想用我的老本行java来实现的.感觉挺简单的,拿lua练一下手吧!
源文本如下:
column XH format a9
column XM format a9
column XB format a4
column XH format a9
column XM format a9
只要是前半部分是一样的(如:column XH format ),就认为是重复的,保留一行就行.
结果应该是:
column XM format a9
column XH format a9
column XB format a4
代码
function file_exists(file)
local f = io.open(file, "rb")
if f then
f:close()
end
return f ~= nil
end
function lines_from(file)
if not file_exists(file) then
return {}
end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
local file = "/home/vncuser/.wp/oracle/test.sql"
local lines = lines_from(file)
local dicLines = {}
local resultLines = {}
for k, v in pairs(lines) do
local index, _ = string.find(v, "format")
local qz = string.sub(v, 0, index - 2)
if not dicLines[qz] then
resultLines[#resultLines + 1] = v
end
dicLines[qz] = true
end
file = io.open("/home/vncuser/.wp/oracle/test_result.sql", "w")
local str = table.concat(resultLines, "\n")
file:write(str)
file:close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)