RosettaCodeData/Task/Remove-lines-from-a-file/Lua/remove-lines-from-a-file.lua

19 lines
502 B
Lua
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
function remove( filename, starting_line, num_lines )
2026-04-30 12:34:36 -04:00
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
2023-07-01 11:58:00 -04:00
end
i = i + 1
2026-04-30 12:34:36 -04:00
end
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
if i > starting_line and i < starting_line + num_lines then
print( "Warning: Tried to remove lines after EOF." )
end
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
local fp = io.open( filename, "w+" ) or error"Not writable"
fp:write( table.concat( content ) )
fp:close()
2023-07-01 11:58:00 -04:00
end