Lua - 将 .txt 的项目列表转换为表格以便迭代

我有一个长的系统生成的代码列表(在 text.txt 文件中),我无法想出如何将它们全部转换为适当的结构,以便我可以逐行迭代。


Power
0000 0070 0000 0032 0080 0040 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0ACD

Power$1
0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 05F7 015B 0057 0016 0E6C

Power$2
0000 0068 0000 0022 0169 00B4 0017 0044 0017 0044 0017 0017 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0044 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0636

等等

我理想地需要以下顺序。

  1. 读取/记录命令的名称(将其写入日志/另一个 txt 文件)
  2. 传输相应的代码(更新日志/txt,显示已发送)
  3. 等待短时间
  4. 移动到列表中的下一个内容。
点赞
用户6676439
用户6676439

非常感谢您的帮助,我使用了提供的示例并将其调整为我的具体需求。

local lfs = require "lfs"
local read_file_path = "/mnt/nas/vera/text1.txt" -- # 在此处填写文件路径
local write_file_path = "/mnt/nas/vera/text2.txt" -- # 在此处填写文件路径
local pattern1 = "^.*Power.*$" -- # 要查找的模式
local pattern2 = "^.*0000.*$" -- # 要查找的模式

local logFile = io.open(write_file_path, 'w')
local logs = {} -- 我们将使用它来将消息记录到 log.txt 中

local i = 1;
for line in io.lines(read_file_path) do
    if (line:find(pattern1) or not line:find('%A')) and line:len() > 1 then
        local powerref = line
        logs[#logs+ 1] = 'Command: ' .. powerref
elseif line:find(pattern2) then
        local ircode = line
        local x = 1
        repeat
            print('Command ' .. x .. ' is "' .. ircode .. '" found on line ' .. i)
            x = x + 1
            local pause = os.clock()
            repeat until os.clock() > pause + 1
        until i > 10 -- 例如,仅打印前 10 个匹配行
        logs[#logs + 1] = 'Sequence: ' .. line
    end
    i = i + 1 -- 增加行号
end

logFile:write(table.concat(logs, '\n'))
logFile:flush()
2020-10-26 08:54:32