In LUA, I would need to read a text file from a line with a specific content to another line with a specific content. How can I do it please?
Here an example
a text file called: aaa.txt
...
...
...
[Main from here on]
line1
line2
line3
...
...
title=Till here
...
...
So I need to count the lines between and starting from the square brakets line [Main from here on] (it's titled so), till the last line called "title=Till here"
This solution is based on io.lines ()
iterator :
--[[
This function will return all lines from <file>
from <from> till <to> or end of file
both ends included.
--]]
local function readFromTo (file, from, to)
io.input (file) -- open file.
local started = false
local lines = {}
for line in io.lines () do
if not started and line == from then
started = true
end
if started then
lines [#lines + 1] = line
if line == to then
-- <to> found:
return lines
end
end
end
-- Only if <to> not found:
return lines
end
print (table.concat (readFromTo ('aaa.txt', '[Main from here on]', 'title=Till here'), '\n'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.