2

我找不到这样做的方法(尝试通过 StackOverflow 搜索)。

我有一个格式化的 xml 文件,我必须按原样从中提取一个块(不修改格式、制表符、<、> 等。)例如,假设有一个文件名为some.xml

该块由以下内容分隔:

 <!- Let us call this tag_begin -->
 <Ask Ref_Ask="XXXYYYYY">   
   ...
 <!- Let us call this tag_end -->
 </Ask>  

我成功提取了 blockend 标记的开始行和结束行,但我不能将所有行长度都放入 result.txt 文件中:行停止在 127 个字符长度处:怎么了?

@echo off

Set Tag_Begin="<Ask Ref_Ask="
Set Tag_End="</Ask>"
set NB=XXXYYYY
set /A CPTE=0
set Line_Begin=
set Line_End=


Rem --- Find all possible start of block lines, and extract the good one set to Line_Begin
findstr /n /C:%Tag_Begin% some.xml | find /i "%NB%"> temporary.txt
for /f "tokens=1 delims=:" %%L in (temporary.txt) do set Line_Begin=%%L

Rem --- Finding the line order (from all possible start lines) which match the good one
findstr /n /C:%Tag_Begin% some.xml | findstr /n "%NB%"> temporary.txt
for /f "tokens=1 delims=:" %%O in (temporary.txt) do set order_begin=%%O
set /A order_begin-=1


Rem -- Looking for all possible end of block lines, and extract the "order_begin" one for Line_End
for /f "skip=%order_begin% tokens=1 delims=:" %%F in ('findstr /n /C:%Tag_End% some.xml') do set Line_End=%%F & goto away
:away


setlocal enabledelayedexpansion
for /f %%L in (some.xml) do (
set /A CPTE+=1
echo CPTE=!CPTE!
if !CPTE! GEQ %Line_Begin% if !CPTE! LEQ %Line_End% echo %%L >> result.txt
)
setlocal disabledelayedexpansion

del temporary.txt
4

2 回答 2

0

未测试:

        @echo off
        for /f "tokens=1 delims=:" %%L in ('findstr /n "<!- Let us call this begin tag -->" ssome.xml') do ( 
         set begin_line=%%L
        )

        for /f "tokens=1 delims=:" %%L in ('findstr /n "<!- Let us call this end tag -->" some.xml') do ( 
         set /a end_line=%%L+1
        )

        echo showing lines between %end_line% and %begin_line%
        break>"%temp%\empty"
        fc "%temp%\empty" "some.xml" /lb  %end_line% /t |more +4 | findstr /B /E /V "*****" | more +%begin_line%
        del /Q /F "%temp%\empty"

用您的文件更改some.xml三个位置。

于 2013-10-16T14:33:04.383 回答
0

findrepl.bat这使用从 - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697调用的帮助程序批处理文件

放在findrepl.bat与批处理文件相同的文件夹中。

@echo off
type some.xml|findrepl "<Ask Ref_Ask=.XXXYYYYY.>" /e:"</Ask>" >newfile.xml
于 2013-10-18T08:16:09.720 回答