Skip to content

Instantly share code, notes, and snippets.

@pelevesque
Last active April 19, 2017 18:28
Show Gist options
  • Save pelevesque/263c5bc8289a5f8fd5881483ff18a245 to your computer and use it in GitHub Desktop.
Save pelevesque/263c5bc8289a5f8fd5881483ff18a245 to your computer and use it in GitHub Desktop.
A Lua script that removes lines from a file.
#!/usr/bin/lua
--[[
Removes lines from a file
@param string filepath
@param num startline (line number where deletion starts)
@param num numlines (number of lines to delete)
ex: $ rmlines ~/myfile 45 7
--]]
local params = {...}
local filename = params[1]
local startline = tonumber(params[2])
local numlines = tonumber(params[3])
local fp = io.open(filename, "r")
if fp == nil then return nil end
local content = {}
local i = 1;
for line in fp:lines() do
if i < startline or i >= startline + numlines then
content[#content+1] = line
end
i = i + 1
end
fp:close()
fp = io.open(filename, "w+")
for i = 1, #content do
fp:write(string.format("%s\n", content[i]))
end
fp:close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment