2016-02-21 128 views
0

我需要从2个分隔符之间的文件中提取文本,并将其复制到TXT文件中。此文本看起来像XML代码,而不是分隔符<string> text... </string>,我有:::SOURCE text .... ::::SOURCE。正如你在第一个分隔符中看到的是':'的3倍,而在第二个是':'的4倍。批处理 - 使用分隔符从文件中提取文本

最重要的是在这两个分隔符之间有多行。文本的

实施例:

texttexttexttexttexttexttexttexttext 
texttexttexttext 
:::SOURCE 
just this text 
just this text 
just this text 
just this text 
... 
just this text 
::::SOURCE texttext 
texttexttext 

希望的输出:

just this text 
just this text 
just this text 
just this text 
... 
just this text 
+0

如果你的目标是凑一个日志文件,注意,批量处理大量的日志文件是无效的,即使使用有效的方法在批处理脚本中。您将从流读取器获得更好的性能,例如[GNU'awk'](http://gnuwin32.sourceforge.net/packages/gawk.htm)。请看看[我过去的挣扎](http://stackoverflow.com/questions/15628017/),所以你不会注定要重复它们。我很确定我已经经历了你现在正在经历的事情。 – rojo

+0

@ rojo ca请你提交这个例子的GNU版本? – Andy

+0

你实际上可以用一个班轮来完成,而不需要脚本。 'awk“/^::: SOURCE/{flag = 1; next}/^ :::: SOURCE/{flag = 0}标记”txtfile.txt“会执行此操作。 ([信贷到这篇文章](http://stackoverflow.com/a/17988834/1683264)) – rojo

回答

1

尝试这种情况:

@echo off 
setlocal enabledelayedexpansion 
if exist srcoutput.txt (break > srcoutput.txt) 
set found= 
set markpoint=false 
set /a count=0 
set /a two=2 
for /f "tokens=* delims= " %%a in (source.txt) do (
    if !count! equ %two% goto :EOF 
    echo %%a | findstr /c:":SOURCE" >nul 
    if errorlevel 1 ( 
      set found=false 
      if "!markpoint!"=="true" (
      echo %%a >> srcoutput.txt 
      ) 
     ) else ( 
       set found=true 
      ) 

    if "!found!"=="true" (
     set /a count=count+1 
     set /a division=!count!%%%two% 
     if !division! equ 0 (
     set markpoint=false 
    ) else (
     set markpoint=true 
     ) 
    ) 

) 
:EOF 

对于输入文件的Source.txt其中包含:

texttexttexttexttexttexttexttexttext 
texttexttexttext 
:::SOURCE 
just this text 
just this text 
just this text 
just this text 
... 
just this text 
::::SOURCE texttext 
:::SOURCE 
just this text 
just this text 
just this text 
just this text 
... 
just this text 
::::SOURCE texttext 
texttexttext 
:::SOURCE 
just this text 
just this text 
just this text 
just this text 
... 
just this text 
::::SOURCE texttext 

在srcoutput.txt输出看起来像:

just this text 
just this text 
just this text 
just this text 
... 
just this text 
+0

感谢svasa的回复。也许我错过了什么,但不幸的是这与不: 'texttexttexttexttexttexttexttext texttexttexttexttext文本 ::: SOURCE 文本 文本 文本 文本 ... 文本 :::: SOURCE 文字文字文字'这个文本有多行.... – Andy

+0

我更新了'示例'并添加了'所需输出'以更好地理解。 – Andy

+0

你应该从一开始就这样做! '::: SOURCE'和':::: SOURCE'分隔符是否总是放在行首(如你的例子中)? – Aacini

相关问题