RosettaCodeData/Task/Remove-lines-from-a-file/Lua/remove-lines-from-a-file.lua
2026-04-30 12:34:36 -04:00

18 lines
502 B
Lua

function remove( filename, starting_line, num_lines )
local content = {}
local i = 1
for line in io.lines(filename, "L") do -- errors
if i < starting_line or i >= starting_line + num_lines then
content[#content+1] = line
end
i = i + 1
end
if i > starting_line and i < starting_line + num_lines then
print( "Warning: Tried to remove lines after EOF." )
end
local fp = io.open( filename, "w+" ) or error"Not writable"
fp:write( table.concat( content ) )
fp:close()
end