标题: [文本处理] [已解决]批处理怎样提取合并多行文本? [打印本页]
作者: noe 时间: 2011-6-12 15:53 标题: [已解决]批处理怎样提取合并多行文本?
本帖最后由 noe 于 2011-6-14 08:49 编辑
有文本内容结构类似如下:
Year: 2011
Month: 3
Version: 201103A
STUDENT
Number: 1
Gender: male
Class: A
Advance: yes
STUDENT
Number: 5
Gender: female
Class: A
Advance: yes
Special: A
STUDENT
Number: 16
Gender: male
Class: B
Advance: no
Special: 9
STUDENT
Number: 17
Gender: male
Class: A
Advance: yes
END
每个STUDENT段包含Number, Gender, Class, Advance, Special这些行。其中前面4个必有,Special这行可能没有。
现希望经过操作后,将每个STUDENT信息合并成一行,如下
Number: 1, Gender: male, Class: A, Advance: yes
Number: 5, Gender: female, Class: A, Advance: yes, Special: A
Number: 16, Gender: male, Class: B, Advance: no, Special: 9
Number: 17, Gender: male, Class: A, Advance: yes
求处理方法。能纯批处理最好,不能实现的话使用第三方命令行程序亦可。
感谢。
作者: Hello123World 时间: 2011-6-12 16:48
本帖最后由 Hello123World 于 2011-6-12 17:02 编辑
假设多行文本的文件名为test.txt,
将批处理放在和test.txt同一个文件夹。- @echo off
- (for /f "tokens=*" %%i in (test.txt) do (
- if /i %%i==student (echo %%i) else (
- set /p =%%i <nul
- )
- ))>test1.txt
-
- pause>nul
复制代码
作者: noe 时间: 2011-6-12 18:31
Year: 2011 Month: 3 Version: 201103A STUDENT
Number: 1 Gender: male Class: A Advance: yes STUDENT
Number: 5 Gender: female Class: A Advance: yes Special: A STUDENT
Number: 16 Gender: male Class: B Advance: no Special: 9 STUDENT
Number: 17 Gender: male Class: A Advance: yes END
输出结果与期望的略有不同,但这样的结果已经能让我满意。感谢。
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |