Board logo

标题: [文本处理] 【已解决】求助批处理很奇怪的格式排版输出到CSV中 [打印本页]

作者: zhengwei007    时间: 2024-3-15 16:58     标题: 【已解决】求助批处理很奇怪的格式排版输出到CSV中

本帖最后由 zhengwei007 于 2024-3-17 08:58 编辑

这次有个新的文本格式,所有内容都在一个a.txt中,效果如下:
  1. fighter_begin
  2. skill_begin skill_name=[s_lucky] get_lv=1 lv_up_sp=0 auto_get=true item_needed={}
  3. skill_begin skill_name=[s_common_craft1] get_lv=1 lv_up_sp=0 auto_get=true item_needed={}
  4. skill_begin skill_name=[s_create_common_item1] get_lv=5 lv_up_sp=0 auto_get=true item_needed={}
  5. skill_begin skill_name=[s_weapon_mastery1] get_lv=5 lv_up_sp=160 auto_get=false item_needed={{[adena];200000}}
  6. skill_begin skill_name=[s_armor_mastery11] get_lv=5 lv_up_sp=160 auto_get=false item_needed={{[fishing_ticket];100000}}
  7. fighter_end
  8. warrior_begin
  9. include_fighter
  10. skill_begin skill_name=[s_sword_blunt_mastery_war11] get_lv=20 lv_up_sp=3700 auto_get=false item_needed={}
  11. skill_begin skill_name=[s_polearm_mastery11] get_lv=20 lv_up_sp=3700 auto_get=false item_needed={}
  12. skill_begin skill_name=[s_light_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
  13. skill_begin skill_name=[s_light_armor_mastery_war12] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
  14. skill_begin skill_name=[s_heavy_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false item_needed={}
  15. skill_begin skill_name=[s_mana_burn1] get_lv=56 lv_up_sp=125000 auto_get=false item_needed={{[sb_mana_burn1];1}}
  16. skill_begin skill_name=[s_erase11] get_lv=56 lv_up_sp=125000 auto_get=false item_needed={{[sb_erase1];1}}
  17. warrior_end
  18. fishing_begin
  19. skill_begin skill_name=[s_fishing_cast] get_lv=1 lv_up_sp=0 auto_get=false item_needed={{[adena];1000}}
  20. skill_begin skill_name=[s_fishing_mastery1] get_lv=1 lv_up_sp=0 auto_get=false item_needed={{[adena];10}}
  21. skill_begin skill_name=[s_polearm_mastery62] get_lv=40 lv_up_sp=13000 auto_get=false item_needed={}
  22. 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两个字段。
最终效果如下:
  1. fighter_begin skill_name=[s_lucky] get_lv=1 lv_up_sp=0 auto_get=true
  2. fighter_begin skill_name=[s_common_craft1] get_lv=1 lv_up_sp=0 auto_get=true
  3. fighter_begin skill_name=[s_create_common_item1] get_lv=5 lv_up_sp=0 auto_get=true
  4. fighter_begin skill_name=[s_weapon_mastery1] get_lv=5 lv_up_sp=160 auto_get=false adena 200000
  5. fighter_begin skill_name=[s_armor_mastery11] get_lv=5 lv_up_sp=160 auto_get=false fishing_ticket 100000
  6. warrior_begin skill_name=[s_sword_blunt_mastery_war11] get_lv=20 lv_up_sp=3700 auto_get=false
  7. warrior_begin skill_name=[s_polearm_mastery11] get_lv=20 lv_up_sp=3700 auto_get=false
  8. warrior_begin skill_name=[s_light_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false
  9. warrior_begin skill_name=[s_light_armor_mastery_war12] get_lv=20 lv_up_sp=1900 auto_get=false
  10. warrior_begin skill_name=[s_heavy_armor_mastery_war11] get_lv=20 lv_up_sp=1900 auto_get=false
  11. warrior_begin skill_name=[s_mana_burn1] get_lv=56 lv_up_sp=125000 auto_get=false sb_mana_burn1 1
  12. warrior_begin skill_name=[s_erase11] get_lv=56 lv_up_sp=125000 auto_get=false sb_erase1 1
  13. fishing_begin skill_name=[s_fishing_cast] get_lv=1 lv_up_sp=0 auto_get=false adena 1000
  14. fishing_begin skill_name=[s_fishing_mastery1] get_lv=1 lv_up_sp=0 auto_get=false adena 10
  15. fishing_begin skill_name=[s_polearm_mastery62] get_lv=40 lv_up_sp=13000 auto_get=false
  16. 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 )来实现:
  1. 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

此帖比前帖直截了当...
  1. @echo off &setlocal enabledelayedexpansion
  2. (for /f "tokens=1-6 delims= " %%1 in (a.txt) do (
  3. set "a=%%1"&if "%%2"=="" (if /i "!a:~-6!"=="_begin" (set "h=%%1")) else (
  4. set "v6=%%6"&if "!v6:~-2!"=="{}" (echo,!h! %%2 %%3 %%4 %%5) else (
  5. for /f "tokens=1-3 delims={[=;]}" %%a in ("%%6") do (echo,!h! %%2 %%3 %%4 %%5 %%b %%c)))
  6. ))>"rec.csv"
  7. endlocal&exit/b
复制代码





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