Hi everyone! =D
I'm wondering how I can use io or fs to delete a line in a file. I know how to make lines and things but I don't see how to delete a line. Can someone help?
Deleting line
Started by lieudusty, Sep 04 2012 01:55 AM
3 replies to this topic
#1
Posted 04 September 2012 - 01:55 AM
#2
Posted 04 September 2012 - 02:12 AM
I'd go about this by reading the entire file, taking each line and putting it in a table, then removing whichever line you want from the table, and rewriting all of the lines in a table.
--get file content
local file = fs.open('filepath','r')
local filedata = file.readAll()
file.close()
--throw lines in a table
local tLines = {}
for line in filedata:gmatch('.-n') do
table.insert(tLines,line)
end
--set up function for deleting specified line
--make it so that it can take multiple lines to delete
local function deleteLine(...)
if type(arg[1]) == 'table' then
for _,v in pairs(arg[1]) do
table.remove(tLines,v)
end
else
for _,v in pairs(arg) do
if type(v) == 'number' then
table.remove(tLines,v)
end
end
end
end
--delete lines as you please
deleteLine(1,2,3)
--rewrite all of the lines (assuming you haven't redefined the file variable)
file.open('w')
local s = ''
for _,v in pairs(tLines) do
s = s..v
end
file.write(s)
file.close()
#3
Posted 04 September 2012 - 02:24 AM
One method that I thought fairly quickly would be to split the file into a table by lines. You could use the split method provided at http://lua-users.org/wiki/SplitJoin
With the above method, we can split a string by the passed pattern into a table of every instance.
We can now split by the end line character: "n"
The file that we want to remove line 5 from: myFile
Hope I helped!
/>
-- Compatibility: Lua-5.1
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
With the above method, we can split a string by the passed pattern into a table of every instance.
We can now split by the end line character: "n"
The file that we want to remove line 5 from: myFile
This is a sexy test!The code we'll write to delete line 5: 'sexy'
local tLines = {}
local nLine = 5 -- Say we don't want line 5.'
local file = fs.open( "myFile", "r" )
local fileContents = file.readAll()
file.close()
-- Compatibility: Lua-5.1
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
tLines = split( fileContents, "n" ) -- Split the file string into a table by lines.
local sFile = "" -- This will be the appended file.
for index,value in ipairs( tLines ) do
if index ~= nLine then -- If we're not on line 5.'
sFile = sFile .. value .. "n" -- Read the next line in and add an end line character to it.
end
end
file = fs.open( "myFile", "w" )
file.write( sFile ) -- Write the new contents into the file.
file.close()
The newly appended file: myFileThis is a test!
Hope I helped!
#4
Posted 04 September 2012 - 03:02 AM
Thanks guys!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











