Board logo

标题: [文本处理] 批处理怎样提取某个字符串作为前缀? [打印本页]

作者: 18333970926    时间: 2022-12-2 11:36     标题: 批处理怎样提取某个字符串作为前缀?

{ 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 = { {  } }         }
作者: Batcher    时间: 2022-12-2 13:43

回复 1# 18333970926


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

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
作者: 18333970926    时间: 2022-12-2 13:59

回复 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
作者: pd1    时间: 2022-12-2 14:58

本帖最后由 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}
复制代码

作者: hfxiang    时间: 2022-12-2 17:38

回复 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
复制代码

作者: 18333970926    时间: 2022-12-5 09:41

回复 5# hfxiang


    下载了,运行完什么效果也没看到的
作者: 18333970926    时间: 2022-12-5 11:00

回复 4# pd1


    我试了,好像不行,方便的话能具体说说吗,谢谢了
作者: Batcher    时间: 2022-12-5 12:12

回复 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%"
复制代码

作者: hfxiang    时间: 2022-12-5 13:21

回复 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即能看到结果
作者: pd1    时间: 2022-12-5 13:33

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

回复 9# hfxiang


    楼主的原始文件是UTF-8编码
作者: 18333970926    时间: 2022-12-5 14:36

回复 9# hfxiang


    确实,txt转成ANSI格式就可以了,谢谢了
作者: hfxiang    时间: 2022-12-5 17:37

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

回复 11# Batcher


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

awk,万能好用




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2