[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理怎样提取某个字符串作为前缀?

{ ID = 100, Name = "黑猫警长", EmptyName = "", IconID = 0, NumericalRate = 0.6, ItemID = 100, BuffID = { 10 }, Pos = { X= 100, Y= 89 }, Type = 0, SkillID = { 4, 6, 7 }, AttackParam = { Value= 1 }, SingleStruct = { HP= 100, AttackRate= 1.2 }, StrStruct = { { HP= 3, ExType= 0 }, { HP= 10, ExType= 1 } }         },
{ ID = 101, Name = "葫芦\n娃", EmptyName = "", IconID = 0, NumericalRate = 0.8, ItemID = 100, BuffID = { 3, 1 }, Pos = {  }, Type = 2, SkillID = { 1 }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
{ ID = 102, Name = "舒\"克\"", EmptyName = "", IconID = 0, NumericalRate = 0.7, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 3, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
{ ID = 103, Name = "贝\n塔", EmptyName = "", IconID = 0, NumericalRate = 0, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 1, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
{ ID = 104, Name = "邋遢大王", EmptyName = "", IconID = 0, NumericalRate = 1, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 2, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         }


怎么将上面的内容替换成
[100] = { ID = 100, Name = "黑猫警长", EmptyName = "", IconID = 0, NumericalRate = 0.6, ItemID = 100, BuffID = { 10 }, Pos = { X= 100, Y= 89 }, Type = 0, SkillID = { 4, 6, 7 }, AttackParam = { Value= 1 }, SingleStruct = { HP= 100, AttackRate= 1.2 }, StrStruct = { { HP= 3, ExType= 0 }, { HP= 10, ExType= 1 } }         },
[101] = { ID = 101, Name = "葫芦\n娃", EmptyName = "", IconID = 0, NumericalRate = 0.8, ItemID = 100, BuffID = { 3, 1 }, Pos = {  }, Type = 2, SkillID = { 1 }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
[102] = { ID = 102, Name = "舒\"克\"", EmptyName = "", IconID = 0, NumericalRate = 0.7, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 3, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
[103] = { ID = 103, Name = "贝\n塔", EmptyName = "", IconID = 0, NumericalRate = 0, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 1, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
[104] = { ID = 104, Name = "邋遢大王", EmptyName = "", IconID = 0, NumericalRate = 1, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 2, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         }

回复 1# 18333970926


具体规律是什么?
如果需要上传文件,可以用阿里云盘或百度网盘。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher


    local tab = {
        Sample = {
                { ID = 100, Name = "黑猫警长"},
                { ID = 102, Name = "舒克"},
                { ID = 103, Name = "贝塔"},
                { ID = 104, Name = "邋遢大王"}
        }

}


-- ID
tab.SampleByID = {}
for _, rec in pairs(tab.Sample) do
        tab.SampleByID[rec.ID] = rec
end

-- Name
tab.SampleByName = {}
for _, rec in pairs(tab.Sample) do
        tab.SampleByName[rec.Name] = rec
end

tab.Enum = {
        ActorType = {
                [0] = "Leader",
                [2] = "Pig",
                Monkey = 1,
                Hammer = 3,
        },
}

return tab


以上是一个txt文本,我想从整个文本中将Sample表里的数据提取出来,然后一它的ID为前缀拼接一下成为以下数据

[100] = { ID = 100, Name = "黑猫警长"},
[101] = { ID = 102, Name = "舒克"},
[101] = { ID = 103, Name = "贝塔"},
[101] = { ID = 104, Name = "邋遢大王"}

我目前只能实现提取出来,不知道怎么写入他的前缀,就是[100]=  这种
这是我目前写的
chcp 65001
@echo off&setlocal enabledelayedexpansion
cd .
set list=%cd%
for /f "delims=" %%D in ('dir /a:d /b') do set list=!list! %%~fD
for %%a in (%list%) do (
        echo return{ > a.txt
        for %%p in (%%a\*.lua) do (
                findstr /c:"{ ID =" %%p >> a.txt
        )
        echo } >> a.txt
)
pause

TOP

本帖最后由 pd1 于 2022-12-2 14:59 编辑

文本存为test.txt文件,我win11默认utf8了就没改
  1. <# :
  2. @echo off
  3. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0' |Out-String|Invoke-Expression"
  4. pause
  5. #>
  6. $text=gc .\test.txt -Encoding UTF8
  7. $reg=[regex]'{ ID = (\d+), Name = ".*?"},?'
  8. $reg.Matches($text)|%{"[{0}] = {1}" -f $_.Groups[1].Value,$_.Groups[0].Value}
复制代码

TOP

回复 1# 18333970926


   
确保输入文件1.txt为ANSI编码格式,下载gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe ),执行
  1. gawk -F"[, ]+" "{print \"[\"$3\"] = \"$0}" 1.txt>2.txt
复制代码

TOP

回复 5# hfxiang


    下载了,运行完什么效果也没看到的

TOP

回复 4# pd1


    我试了,好像不行,方便的话能具体说说吗,谢谢了

TOP

回复 1# 18333970926


我看了你的 a.lua 文件是UTF-8编码
请参考Q-04把bat文件保存为UTF-8编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA
  1. @echo off
  2. chcp 65001
  3. cd /d "%~dp0"
  4. REM 查找哪个文件
  5. set "FromFile=a.lua"
  6. REM 结果写入哪个文件
  7. set "ToFile=b.txt"
  8. (for /f "tokens=1-4* delims=, " %%a in ('findstr /c:"{ ID =" "%FromFile%"') do (
  9.     echo [%%d] = %%a %%b %%c %%d, %%e
  10. ))>"%ToFile%"
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 5# hfxiang


需将
  1. {ID = 100, Name = "黑猫警长", EmptyName = "", IconID = 0, NumericalRate = 0.6, ItemID = 100, BuffID = { 10}, Pos = { X= 100, Y= 89 }, Type = 0, SkillID = { 4, 6, 7 }, AttackParam = { Value= 1 }, SingleStruct = { HP= 100, AttackRate= 1.2 }, StrStruct = { { HP= 3, ExType= 0 }, { HP= 10, ExType= 1 } }         },
  2. {ID = 101, Name = "葫芦 \n 娃", EmptyName = "", IconID = 0, NumericalRate = 0.8, ItemID = 100, BuffID = { 3, 1}, Pos = {  }, Type = 2, SkillID = { 1 }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
  3. {ID = 102, Name = "舒 \" 克 \"", EmptyName ="", IconID = 0, NumericalRate = 0.7, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 3, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
  4. {ID = 103, Name = "贝 \n 塔", EmptyName = "", IconID = 0, NumericalRate = 0, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 1, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         },
  5. {ID = 104, Name = "邋遢大王", EmptyName = "", IconID = 0, NumericalRate = 1, ItemID = 100, BuffID = {  }, Pos = {  }, Type = 2, SkillID = {  }, AttackParam = {  }, SingleStruct = { HP= 10, AttackRate= 0, ExType= 0 }, StrStruct = { {  } }         }
复制代码
以ANSI编码格式保存为1.txt
再执行
  1. gawk -F"[, ]+" "{print \"[\"$3\"] = \"$0}" 1.txt>2.txt
复制代码
打开2.txt即能看到结果

TOP

回复 7# 18333970926
这还不具体?
你直接一个好像不行我没法分析,我肯定是测试通过了才可能发出来的。
你把不行的内容贴出来,你得原文本和运行时的情景。

TOP

回复 9# hfxiang


    楼主的原始文件是UTF-8编码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 9# hfxiang


    确实,txt转成ANSI格式就可以了,谢谢了

TOP

本帖最后由 hfxiang 于 2022-12-19 18:04 编辑

回复 11# Batcher


   
UTF-8 编码 就需要用Ruby(https://rubyinstaller.org/downloads/)中附带的awk方能处理 。除此之外,经测试好像其它的awk都不能满足对UTF-8 编码的处置。

TOP

awk,万能好用

TOP

返回列表