You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Lua脚本提取ID_STATUS.txt中指定十六进制ID并覆写文件

Lua脚本实现提取指定格式ID并覆写文件

以下是满足需求的完整Lua脚本,基于你提供的代码框架补充实现:

-- 读取目标文件
local f = io.open("ID_STATUS.txt", "r")
if f == nil then
    print("   Could not read file ")
    return nil
end

local extractedID = nil
-- 逐行扫描文件内容
for line in f:lines() do
    -- 匹配指定格式的行,捕获10位十六进制ID
    local id = string.match(line, "^%[%+%] EM 410x ID ([0-9A-Fa-f]{10})$")
    if id then
        extractedID = id:upper() -- 统一转为大写(可选,根据需求调整)
        break -- 找到目标行后停止扫描
    end
end
f:close()

-- 检查是否提取到有效ID
if not extractedID then
    print("   No valid ID found in file ")
    return nil
end

-- 覆写文件写入提取到的ID
local f = io.open("ID_STATUS.txt", "w+")
if f == nil then
    print("   Could not write to file ")
    return nil
end
f:write(extractedID .. "\n") -- 写入ID并换行
f:close()

print("   Successfully extracted and wrote ID: " .. extractedID)

关键实现说明

  • 正则匹配:使用string.match匹配格式为[+] EM 410x ID XXXXXXXXXX的行,其中([0-9A-Fa-f]{10})用于捕获10位十六进制字符(支持大小写)
  • ID提取:找到匹配行后提取捕获的ID,可选转为统一大写(符合你示例中的AD23BC7860格式)
  • 文件操作:先以只读模式读取扫描,再以写入模式覆写原文件,确保操作安全
  • 异常处理:增加了文件写入失败、未找到有效ID的提示逻辑

内容的提问来源于stack exchange,提问作者jarecki b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 00:09:57