标题: [文本处理] 【已解决】求助批处理很奇怪的格式排版输出到CSV中 [打印本页]
作者: zhengwei007 时间: 2024-3-15 16:58 标题: 【已解决】求助批处理很奇怪的格式排版输出到CSV中
本帖最后由 zhengwei007 于 2024-3-17 08:58 编辑
这次有个新的文本格式,所有内容都在一个a.txt中,效果如下:- fighter_begin
- skill_begin skill_name=[s_lucky] get_lv=1 lv_up_sp=0 auto_get=true item_needed={}
- skill_begin skill_name=[s_common_craft1] get_lv=1 lv_up_sp=0 auto_get=true item_needed={}
- skill_begin skill_name=[s_create_common_item1] get_lv=5 lv_up_sp=0 auto_get=true item_needed={}
- skill_begin skill_name=[s_weapon_mastery1] get_lv=5 lv_up_sp=160 auto_get=false item_needed={{[adena];200000}}
- skill_begin skill_name=[s_armor_mastery11] get_lv=5 lv_up_sp=160 auto_get=false item_needed={{[fishing_ticket];100000}}
- fighter_end
- warrior_begin
- include_fighter
- skill_begin skill_name=[s_sword_blunt_mastery_war11] get_lv=20 lv_up_sp=3700 auto_get=false item_needed={}
- skill_begin skill_name=[s_polearm_mastery11] get_lv=20 lv_up_sp=3700 auto_get=false item_needed={}
- skill_begin skill_name=[s_light_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
- skill_begin skill_name=[s_light_armor_mastery_war12] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
- skill_begin skill_name=[s_heavy_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
- skill_begin skill_name=[s_mana_burn1] get_lv=56 lv_up_sp=125000 auto_get=false item_needed={{[sb_mana_burn1];1}}
- skill_begin skill_name=[s_erase11] get_lv=56 lv_up_sp=125000 auto_get=false item_needed={{[sb_erase1];1}}
- warrior_end
- fishing_begin
- skill_begin skill_name=[s_fishing_cast] get_lv=1 lv_up_sp=0 auto_get=false item_needed={{[adena];1000}}
- skill_begin skill_name=[s_fishing_mastery1] get_lv=1 lv_up_sp=0 auto_get=false item_needed={{[adena];10}}
- skill_begin skill_name=[s_polearm_mastery62] get_lv=40 lv_up_sp=13000 auto_get=false item_needed={}
- skill_begin skill_name=[s_polearm_mastery63] get_lv=40 lv_up_sp=13000 auto_get=false item_needed={}
复制代码
我梳理了一下整理排版步骤:
1、将所有带_begin的,放在下面的skill_begin每一行前面。
2、删除最后item_needed = {}的空的字符串。
3、将item_needed有内容的字符串自动整理成adena 200000两个字段。
最终效果如下:- fighter_begin skill_name=[s_lucky] get_lv=1 lv_up_sp=0 auto_get=true
- fighter_begin skill_name=[s_common_craft1] get_lv=1 lv_up_sp=0 auto_get=true
- fighter_begin skill_name=[s_create_common_item1] get_lv=5 lv_up_sp=0 auto_get=true
- fighter_begin skill_name=[s_weapon_mastery1] get_lv=5 lv_up_sp=160 auto_get=false adena 200000
- fighter_begin skill_name=[s_armor_mastery11] get_lv=5 lv_up_sp=160 auto_get=false fishing_ticket 100000
- warrior_begin skill_name=[s_sword_blunt_mastery_war11] get_lv=20 lv_up_sp=3700 auto_get=false
- warrior_begin skill_name=[s_polearm_mastery11] get_lv=20 lv_up_sp=3700 auto_get=false
- warrior_begin skill_name=[s_light_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false
- warrior_begin skill_name=[s_light_armor_mastery_war12] get_lv=20 lv_up_sp=1900 auto_get=false
- warrior_begin skill_name=[s_heavy_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false
- warrior_begin skill_name=[s_mana_burn1] get_lv=56 lv_up_sp=125000 auto_get=false sb_mana_burn1 1
- warrior_begin skill_name=[s_erase11] get_lv=56 lv_up_sp=125000 auto_get=false sb_erase1 1
- fishing_begin skill_name=[s_fishing_cast] get_lv=1 lv_up_sp=0 auto_get=false adena 1000
- fishing_begin skill_name=[s_fishing_mastery1] get_lv=1 lv_up_sp=0 auto_get=false adena 10
- fishing_begin skill_name=[s_polearm_mastery62] get_lv=40 lv_up_sp=13000 auto_get=false
- fishing_begin skill_name=[s_polearm_mastery63] get_lv=40 lv_up_sp=13000 auto_get=false
复制代码
将以上结果输出到rec.csv中即可。这和之前我提供过的内容样式都不同,不知是否比较复杂,谢谢各位。
作者: hfxiang 时间: 2024-3-15 17:38
回复 1# zhengwei007
可借助第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )来实现:- gawk -v"OFS=\t" "/_begin[ \t]*$/,/_end[ \t]*$/{if(/_begin[ \t]*$/){A=$1;next};if(/_end[ \t]*$/){next};if($NF~/[{}]+/){patsplit($NF, b, /[^\[\];\{\}]+/);$NF=\"\";$1=\"\";$0=$0;print A $0 b[2],b[3]}}" a.txt>rec.csv
复制代码
作者: zhengwei007 时间: 2024-3-15 18:36
回复 zhengwei007
可借助第3方工具gawk( )来实现:
hfxiang 发表于 2024-3-15 17:38
太强大了,非常感谢!
作者: aloha20200628 时间: 2024-3-15 19:22
本帖最后由 aloha20200628 于 2024-3-15 19:24 编辑
回复 1# zhengwei007
此帖比前帖直截了当...- @echo off &setlocal enabledelayedexpansion
- (for /f "tokens=1-6 delims= " %%1 in (a.txt) do (
- set "a=%%1"&if "%%2"=="" (if /i "!a:~-6!"=="_begin" (set "h=%%1")) else (
- set "v6=%%6"&if "!v6:~-2!"=="{}" (echo,!h! %%2 %%3 %%4 %%5) else (
- for /f "tokens=1-3 delims={[=;]}" %%a in ("%%6") do (echo,!h! %%2 %%3 %%4 %%5 %%b %%c)))
- ))>"rec.csv"
- endlocal&exit/b
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |