标题: [文本处理] 【已解决】求助BAT代码中提取几个关键词写到csv中 [打印本页]
作者: zhengwei007 时间: 2024-2-24 11:03 标题: 【已解决】求助BAT代码中提取几个关键词写到csv中
一共有2组,第一组代码如下:- <item npcId="20583"> <!-- Timak Orc -->
- <detail chance="9" skill="true" maxLevel="1" />
- </item>
- <item npcId="20584"> <!-- Timak Orc Archer -->
- <detail chance="9" skill="true" maxLevel="1" />
- </item>
- <item npcId="20585"> <!-- Timak Orc Soldier -->
- <detail chance="9" skill="true" maxLevel="1" />
- </item>
- <item npcId="20586"> <!-- Timak Orc Warrior -->
- <detail chance="9" skill="true" maxLevel="1" />
- </item>
- <item npcId="20587"> <!-- Timak Orc Shaman -->
- <detail chance="9" skill="true" maxLevel="2" />
- </item>
- <item npcId="20588"> <!-- Timak Orc Overlord -->
- <detail chance="9" skill="true" maxLevel="2" />
- </item>
- <item npcId="20625"> <!-- Malruk Knight -->
- <detail chance="9" skill="true" maxLevel="9" />
- </item>
- <item npcId="20626"> <!-- Malruk Berserker -->
- <detail chance="9" skill="true" maxLevel="9" />
- </item>
- <item npcId="20627"> <!-- Malruk Lord -->
- <detail chance="9" skill="true" maxLevel="9" />
- </item>
- <item npcId="20628"> <!-- Limal Karinness -->
- <detail chance="9" skill="true" maxLevel="9" />
- </item>
- <item npcId="20629"> <!-- Karik -->
- <detail chance="9" skill="true" maxLevel="9" />
- </item>
- <item npcId="20636"> <!-- Forest of Mirrors Ghost -->
- <detail chance="9" skill="true" maxLevel="2" />
- </item>
- <item npcId="20637"> <!-- Forest of Mirrors Ghost -->
- <detail chance="9" skill="true" maxLevel="3" />
- </item>
- <item npcId="20638"> <!-- Forest of Mirrors Ghost -->
- <detail chance="9" skill="true" maxLevel="3" />
- </item>
- <item npcId="20639"> <!-- Mirror -->
- <detail chance="9" skill="true" maxLevel="3" />
- </item>
- <item npcId="20640"> <!-- Harit Lizardman -->
- <detail chance="9" skill="true" maxLevel="4" />
- </item>
- <item npcId="20641"> <!-- Harit Lizardman Grunt -->
- <detail chance="9" skill="true" maxLevel="4" />
- </item>
- <item npcId="20642"> <!-- Harit Lizardman Archer -->
- <detail chance="9" skill="true" maxLevel="4" />
- </item>
复制代码
由于显示不下了,所以截取了一部分,但格式都是以上这样的,希望做成如下效果即可:
以上代码存成一个sour.txt,执行批处理后,存成res.txt,用前两个举例内容如下:- npcid chance skill maxlevel
- 20583 9 TRUE 1
- 20584 9 TRUE 1
复制代码
作者: zhengwei007 时间: 2024-2-24 11:11
第二组稍微麻烦一点,有两种,代码如下:- <item npcId="25687"> <!-- Hekaton Prime -->
- <detail chance="5" absorbType="PARTY_ONE_RANDOM" levelList="10, 11, 12, 13, 14" />
- </item>
- </item>
- <item npcId="25677"> <!-- Water Spirit Lian -->
- <detail chance="10" absorbType="PARTY_ONE_RANDOM" levelList="12, 13" />
- <detail chance="5" absorbType="PARTY_ONE_RANDOM" levelList="14" />
- </item>
- <item npcId="25603"> <!-- Darion -->
- <detail chance="10" absorbType="PARTY_ONE_RANDOM" levelList="12, 13" />
- <detail chance="5" absorbType="PARTY_ONE_RANDOM" levelList="14" />
- <detail chance="3" absorbType="PARTY_ONE_RANDOM" levelList="15" />
- </item>
复制代码
上面仅是举例内容,这段就是把chance分开,把levelList分开,方法也是先把源内容存在sour.txt,整理输出后变成如下格式:- npcid chance absorbType level
- 25678 5 PARTY_ONE_RANDOM 10
- 25678 5 PARTY_ONE_RANDOM 11
- 25678 5 PARTY_ONE_RANDOM 12
- 25678 5 PARTY_ONE_RANDOM 13
- 25678 5 PARTY_ONE_RANDOM 14
- 25677 10 PARTY_ONE_RANDOM 12
- 25677 10 PARTY_ONE_RANDOM 13
- 25677 5 PARTY_ONE_RANDOM 14
- 25603 10 PARTY_ONE_RANDOM 12
- 25603 10 PARTY_ONE_RANDOM 13
- 25603 5 PARTY_ONE_RANDOM 14
- 25603 3 PARTY_ONE_RANDOM 15
复制代码
谢谢各位大佬。
作者: aloha20200628 时间: 2024-2-24 12:49
本帖最后由 aloha20200628 于 2024-2-24 19:34 编辑
代码说明》
通用不同组别的源文件;
任取源文件中一个唯一性标识词,如 ' skill=' 用于判断并构建对应组别的输出结果文件头
用1-2楼的两个示例分别测试通过- @echo off &setlocal enabledelayedexpansion
- (findstr " skill=" sour.txt>nul&&echo,npcid chance skill maxlevel||echo,npcid chance absorbType level)>"res.txt"
- (for /f tokens^=1-6^delims^=^" %%1 in (' findstr "=" "sour.txt" ') do (
- if "%%~4"=="" (set "v1=%%2") else for %%v in (%%6) do (echo,!v1! %%2 %%4 %%v)
- ))>>"res.txt"
- endlocal&exit/b
复制代码
作者: zhengwei007 时间: 2024-2-24 14:57
代码说明》
通用不同组别的源文件;
任取源文件中一个唯一性标识词,如 ' skill=' 用于判断并构建对应组 ...
aloha20200628 发表于 2024-2-24 12:49
您好,谢谢,可以使用,完美解决问题。
作者: zhengwei007 时间: 2024-2-24 16:08
代码说明》
通用不同组别的源文件;
任取源文件中一个唯一性标识词,如 ' skill=' 用于判断并构建对应组 ...
aloha20200628 发表于 2024-2-24 12:49
- <item>
- <!-- Great Sword -->
- <ingredient count="1" id="78" />
- <!-- Neolithic Crystal - B -->
- <ingredient count="1" id="14678" />
- <!-- Great Sword - Lightning -->
- <production count="1" id="10870" />
- </item>
- <item>
- <!-- Great Sword - Health -->
- <ingredient count="1" id="4723" />
- <!-- Neolithic Crystal - B -->
- <ingredient count="1" id="14678" />
- <!-- Great Sword - Lightning - Health -->
- <production count="1" id="10871" />
- </item>
复制代码
请问一下,类似这种的,通过你的批处理我想直接修改能达到我这边通用,但好像改了不行,麻烦你帮忙看看这种效果:
78 14678 10870
4723 14678 10871
作者: aloha20200628 时间: 2024-2-24 18:42
本帖最后由 aloha20200628 于 2024-2-24 19:33 编辑
回复 5# zhengwei007
针对5楼示例样本的代码如下- @echo off &setlocal enabledelayedexpansion
- (for /f tokens^=1-4^delims^=^" %%1 in (' findstr "=" "sour.txt" ') do for /f "tokens=1 delims= < " %%a in ("%%~1") do if /i "%%~a"=="production" (echo,!v:~1! %%4&set "v=") else (set "v=!v! %%4")
- )>"res.txt"
- endlocal&exit/b
复制代码
作者: hfxiang 时间: 2024-2-25 14:41
回复 5# zhengwei007
此类问题,俺一般会使用第3方工具gawk(http://bcn.bathome.net/tool/4.1.0/gawk.exe)来处理:
对于1楼的样本:- gawk -v"FS=\042"-v"OFS=\t" "BEGIN{FS=\"\042\";OFS=\"\t\";print\"npcid\tchance\tskill\tmaxlevel\"}/<item npcId=/,/<\/item>/{if(/<item npcId=/)A=$2;if(/<detail chance=/)print A,$2,$4,$6}" L1.txt>Out1.txt
复制代码
对于2楼的样本:- gawk -v"FS=\042"-v"OFS=\t" "BEGIN{FS=\"\042\";OFS=\"\t\";print\"npcid\tchance\tabsorbType\tlevel\"}/<item npcId=/,/<\/item>/{if(/<item npcId=/)A=$2;if(/<detail chance=/){split($6,a,/, /);for(i=1;i in a;i++)print A,$2,$4,a[i]}}" L2.txt>Out2.txt
复制代码
对于5楼的样本:- gawk -v"FS=\042"-v"OFS=\t" "BEGIN{FS=\"\042\";OFS=\"\t\";A=\"\"}/<item>/,/<\/item>/{if(/<[^<]+\/>/){A?A=A\"\t\"$4:A=$4};if(/<\/item>/){print A;A=\"\"}}" L5.txt>Out5.1.txt
复制代码
作者: zhengwei007 时间: 2024-2-25 17:05
回复 zhengwei007
针对5楼示例样本的代码如下
aloha20200628 发表于 2024-2-24 18:42
谢谢,已解决。
作者: zhengwei007 时间: 2024-2-25 17:07
回复 zhengwei007
此类问题,俺一般会使用第3方工具gawk()来处理:
对于1楼的样本:对于2楼的样本: ...
hfxiang 发表于 2024-2-25 14:41
我想尝试修改,然后去获取别的内容,但……根本看不懂,奈何我不是这块料儿~
作者: qixiaobin0715 时间: 2024-2-26 14:43
本帖最后由 qixiaobin0715 于 2024-2-26 16:47 编辑
1楼:- @echo off
- echo,npcid chance skill maxlevel
- set /a _chance=_skill=1
- setlocal enabledelayedexpansion
- for /f "delims=<> " %%i in (sour.txt) do (
- for %%j in (%%i) do (
- if defined x1 (
- set x1=
- set str=%%~j
- ) else if defined x2 (
- set x2=
- echo,!str! %%~j
- ) else if defined x3 (
- set x3=
- set str=!str! %%~j
- ) else (
- if /i "%%j"=="npcid" (
- set x1=true
- ) else if /i "%%j"=="maxlevel" (
- set x2=true
- ) else if defined _%%j (
- set x3=true
- )
- )
- )
- )
- pause
复制代码
2楼:- @echo off
- echo,npcid chance absorbType levellist
- set /a _chance=_absorbType=1
- setlocal enabledelayedexpansion
- for /f "delims=<> " %%i in (sour.txt) do (
- for %%j in (%%i) do (
- if defined x1 (
- set x1=
- set str1=%%~j
- ) else if defined x2 (
- set x2=
- for %%l in (%%~j) do echo,!str1!!str2! %%l
- set str2=
- ) else if defined x3 (
- set x3=
- set str2=!str2! %%~j
- ) else (
- if /i "%%j"=="npcid" (
- set x1=true
- ) else if /i "%%j"=="levellist" (
- set x2=true
- ) else if defined _%%j (
- set x3=true
- )
- )
- )
- )
- pause
复制代码
5楼:- @echo off
- for /f "delims=<> " %%i in (sour.txt) do (
- if "%%i"=="item" (
- setlocal enabledelayedexpansion
- ) else if "%%i"=="/item" (
- echo,!str!
- endlocal
- )
- for %%j in (%%i) do (
- if defined x (
- set x=
- set str=!str!%%~j
- ) else if /i "%%j"=="id" (
- set x=true
- )
- )
- )
- pause
复制代码
代码未经测试,可能会有问题,大体思路应当可行。
作者: zhengwei007 时间: 2024-3-16 17:49 标题: RE: 求助BAT代码中提取几个关键词写到csv中
- <npc id="20583" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Timak Orc -->
- <npc id="20584" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Timak Orc Archer -->
- <npc id="20794" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Blade Stakato -->
- <npc id="20795" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Blade Stakato Worker -->
- <npc id="20796" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Blade Stakato Warrior -->
- <npc id="20797" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Blade Stakato Drone -->
- <npc id="20798" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Water Giant -->
- <npc id="20799" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Queen Undine Lad -->
- <npc id="20800" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Eva's Seeker -->
- <npc id="20801" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Theeder Piker -->
- <npc id="20802" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Theeder Mage -->
- <npc id="20803" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Doll Master -->
- <npc id="20821" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Erin Ediunce -->
- <npc id="20823" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Tribe Soldier -->
- <npc id="20826" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Tribe Archer -->
- <npc id="20827" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Tribe Warrior -->
- <npc id="20828" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Tribe Shaman -->
- <npc id="20829" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Tribe Overlord -->
- <npc id="20830" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Guardian Angel -->
- <npc id="20831" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Seal Angel -->
- <npc id="20848" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Light Bringer -->
- <npc id="20849" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1"/><!-- Light Worm -->
- <npc id="20860" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Seal Angel -->
- <npc id="20986" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Sairon -->
- <npc id="20987" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Sairon's Doll -->
- <npc id="20988" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Sairon's Puppet -->
- <npc id="20994" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Garden Guard Leader -->
- <npc id="20995" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Garden Guard -->
- <npc id="21006" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7"/><!-- Doom Servant -->
- <npc id="21069" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Guardian Prefect -->
- <npc id="21070" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Seal Archangel -->
- <npc id="21071" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Seal Archangel -->
- <npc id="21072" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5;6;7;8;9"/><!-- Platinum Guardian Chief -->
- <npc id="21611" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Unpleasant Humming -->
- <npc id="21612" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2"/><!-- Unpleasant Humming -->
- <npc id="21614" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Death Flyer -->
- <npc id="21615" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Death Flyer -->
- <npc id="21617" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Fiend Archer -->
- <npc id="21620" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Musveren -->
- <npc id="21621" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3"/><!-- Musveren -->
- <npc id="21623" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Zaken's Guard -->
- <npc id="21624" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Zaken's Guard -->
- <npc id="21626" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Kaim Vanul -->
- <npc id="21627" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Kaim Vanul -->
- <npc id="21629" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Pirate Zombie Captain -->
- <npc id="21630" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4"/><!-- Pirate Zombie Captain -->
- <npc id="21632" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Doll Blader -->
- <npc id="21633" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Doll Blader -->
- <npc id="21635" chanceStage="100" chanceBreak="20" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Vale Master -->
- <npc id="21636" chanceStage="150" chanceBreak="0" skill="true" absorbType="LAST_HIT" levelList="0;1;2;3;4;5"/><!-- Vale Master -->
- <npc id="22215" chanceStage="100" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Tyrannosaurus -->
- <npc id="22216" chanceStage="100" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Tyrannosaurus -->
- <npc id="22217" chanceStage="100" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Tyrannosaurus -->
- <npc id="25109" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Antharas Priest Cloe -->
- <npc id="25163" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Roaring Skylancer -->
- <npc id="25202" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Krokian Padisha Sobekk -->
- <npc id="25269" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Beast Lord Behemoth -->
- <npc id="25283" chanceStage="100" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="10;11"/><!-- Lilith -->
- <npc id="25286" chanceStage="100" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="10;11"/><!-- Anakim -->
- <npc id="25319" chanceStage="75" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="12"/><!-- Ember -->
- <npc id="25328" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Eilhalder von Hellmann -->
- <npc id="25337" chanceStage="75" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Anakazel -->
- <npc id="25338" chanceStage="75" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="12"/><!-- Anakazel -->
- <npc id="25453" chanceStage="35" chanceBreak="0" skill="false" absorbType="PARTY_ONE_RANDOM" levelList="10;11"/><!-- Meanas Anor -->
- <npc id="29020" chanceStage="100" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="10;11"/><!-- Baium -->
- <npc id="29022" chanceStage="100" chanceBreak="0" skill="false" absorbType="FULL_PARTY" levelList="10;11"/><!-- Zaken -->
复制代码
请问这代码为什么输出不了上面正确的结果???
作者: aloha20200628 时间: 2024-3-16 18:28
本帖最后由 aloha20200628 于 2024-3-16 19:13 编辑
回复 11# zhengwei007
数据不在同一行...
处理11楼新示例样本数据的代码如下- @echo off &setlocal enabledelayedexpansion
- (for /f tokens^=1-12^delims^=^" %%a in (' findstr "=" "sour.txt" ') do (
- (set "vl=%%l"&echo,%%b %%d %%f %%h %%j !vl:;= !)
- ))>"res.txt"
- endlocal&exit/b
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |