找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23929|回复: 3

[文本处理] 【已解决】求助批处理从一堆文本文件中提取关键词写到csv

[复制链接]
发表于 2024-2-27 09:18:13 | 显示全部楼层 |阅读模式
本帖最后由 zhengwei007 于 2024-2-27 13:25 编辑

我想从100多个文档里面,发现有Soul Crystal字符时,截取<item>这段内容的字段。字段包括每个ID和count数量,单独存放在csv文件里。
文档中是以<item>开头,</item>结尾的,谢谢大家。
由于不能上传附件,我粘点里面内容吧。
  1.         <item>
  2.                 <!-- Stormbringer -->
  3.                 <ingredient count="1" id="72" />
  4.                 <!-- Red Soul Crystal - Stage 5 -->
  5.                 <ingredient count="1" id="4634" />
  6.                 <!-- Gemstone C -->
  7.                 <ingredient count="97" id="2131" />
  8.                 <!-- Adena -->
  9.                 <ingredient count="291000" id="57" />
  10.                 <!-- Stormbringer - Critical Anger -->
  11.                 <production count="1" id="4681" />
  12.         </item>
  13.         <item>
  14.                 <!-- Guardian Sword - Great Gale -->
  15.                 <ingredient count="1" id="10955" />
  16.                 <!-- Adena -->
  17.                 <ingredient count="3390000" id="57" />
  18.                 <!-- Gemstone B -->
  19.                 <ingredient count="339" id="2132" />
  20.                 <!-- Blue Soul Crystal - Stage 10 -->
  21.                 <ingredient count="1" id="4661" />
  22.                 <!-- Guardian Sword - Great Gale - Critical Bleed -->
  23.                 <production count="1" id="10958" />
  24.         </item>
  25.         <item>
  26.                 <!-- Blood Brother - Great Gale -->
  27.                 <ingredient count="1" id="21966" />
  28.                 <!-- Blue Soul Crystal - Stage 14 -->
  29.                 <ingredient count="1" id="9571" />
  30.                 <!-- Gemstone S -->
  31.                 <ingredient count="285" id="2134" />
  32.                 <!-- Blood Brother - Great Gale - Light -->
  33.                 <production count="1" id="21969" />
  34.         </item>
复制代码
每个文件里可能有多个这样的内容结构。
最终csv就是这样:
72        1        4634        1        2131        97        57        291000        4681        1
10955        1        57        3390000        2132        339        4661        1        10958        1
21966        1        9571        1        2134        285        21969        1               
谢谢各位大哥。
发表于 2024-2-27 09:39:12 | 显示全部楼层
回复 1# zhengwei007


请用使用网盘上传原始文件。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
发表于 2024-2-27 10:50:37 | 显示全部楼层
回复 1# zhengwei007

题型类同(http://www.bathome.net/thread-68463-1-1.html)中6楼解法...
先匹配当前目录中所有包含‘Soul Crystal’字段的*.xml文件,再逐一提取目标数据,结果生成与之对应的*.csv
假设源文件*.xml均为utf-8编码,若是ansi或简中编码(gb2312/gbk),则删除第2行代码即可

  1. @echo off &setlocal enabledelayedexpansion
  2. chcp 65001>nul
  3. for /f "delims=" %%F in (' findstr /imc:"Soul Crystal" *.xml') do (
  4.         (for /f tokens^=1-4^delims^=^" %%1 in (' findstr "=" "%%~F" ') do for /f "tokens=1 delims=        < " %%a in ("%%~1") do if /i "%%~a"=="production" (echo,!v:~1!        %%4        %%2&set "v=") else (set "v=!v!        %%4        %%2")
  5.         )>"%%~F.csv"
  6. )
  7. endlocal&exit/b
复制代码
 楼主| 发表于 2024-2-27 13:25:06 | 显示全部楼层
谢谢楼上几位,已经解决问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 05:25 , Processed in 0.018830 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表