#!/opt/Lua51/bin/lua -- Created by jiaqiang.ye@lnxall.com -- Remove duplicated lines in a text file -- 2025/08/07 package.cpath = "/opt/Lua51/lib/lua/?.so;/opt/Lua51/lib/lua/loadall.so" package.path = "/opt/Lua51/share/lua/?.lua;/opt/Lua51/share/lua/?/init.lua;/opt/Lua51/lib/lua/?.lua;/opt/Lua51/lib/lua/?/init.lua" local invoker = require "invoker" local gfmt = string.format local function dedup_file(filp) local typ, fsize = invoker.statfile(filp) if typ == nil then return false end if typ ~= "regular" or fsize >= 0x100000 then io.stderr:write(gfmt("Error, invalid file type or size: %s\n", filp)) io.stderr:flush() return false end local fp = io.open(filp, "rb") if not fp then io.stderr:write(gfmt("Error, failed to open file: %s\n", filp)) io.stderr:flush() return false end local lines, lmap, nline, dowrite = {}, {}, 0, 0 while true do local nl = fp:read("*l") if nl == nil then break end if #nl == 0 then dowrite = dowrite + 1 elseif lmap[nl] then dowrite = dowrite + 1 else lmap[nl] = true nline = nline + 1 lines[nline] = nl end end fp:close() if dowrite == 0 then return true end if nline > 1 then table.sort(lines) end io.stdout:write(gfmt("Updating text file: %s ...\n", filp)) io.stdout:flush() fp = io.open(filp, "wb") if not fp then io.stderr:write(gfmt("Error, failed to open file for writing: %s\n", filp)) io.stderr:flush() return false end if nline > 1 then local wd = table.concat(lines, "\n") fp:write(wd) fp:write("\n") else fp:write(lines[1] .. "\n") end fp:flush() fp:close() return true end os.exit(dedup_file(arg[1]) and 0 or 1)