Batch file 从现有文本文件中查找和提取文本

Batch file 从现有文本文件中查找和提取文本,batch-file,extract,text-files,Batch File,Extract,Text Files,我需要能够从现有的文本文件中提取数据。文本文件的结构如下所示 this line contains a type of header and always starts at column 1 this line contains other data and is always tabbed in this line contains other data and is always tabbed in this line contains other data

我需要能够从现有的文本文件中提取数据。文本文件的结构如下所示

this line contains a type of header and always starts at column 1
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in

this line contains a type of header and always starts at column 1
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in

this line contains a type of header and always starts at column 1
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in

this line contains a type of header and always starts at column 1
     this line contains other data and is always tabbed in
     this line contains other data and is always tabbed in
如您所见,文本文件按节排列。始终有一个标题行,后面跟着随机数目的其他数据行,各节之间始终有一个空行。不幸的是,标题部分的命名方案或其他数据行中包含的数据没有押韵或理由……只有上述结构在某种程度上是一致的。我需要搜索的数据位于另一个数据行中,仅位于其中一个部分中,可以位于文本文件中的任何位置。我可以使用FIND命令来定位需要查找的文本,但一旦这样做,我就需要能够将整个部分提取到一个新的文本文件中。我不知道如何向上移动多少行到前面的第一个空行,然后向下移动到后面的下一个空行,并提取中间的所有内容。这有意义吗?不幸的是,VBScript根本不是这个应用程序的选项,否则它早就结束了。有什么想法吗?Thanx.

@echo off
@echo off
setlocal enableDelayedExpansion
set input="test.txt"
set output="extract.txt"
set search="MY TEXT"

::find the line with the text
for /f "delims=:" %%N in ('findstr /n /c:!search! %input%') do set lineNum=%%N
set "begin=0"

::find blank lines and set begin to the last blank before text and end to the first blank after text
for /f "delims=:" %%N in ('findstr /n "^$" %input%') do (
  if %%N lss !lineNum! (set "begin=%%N") else set "end=%%N" & goto :break
)
::end of section not found so we must count the number of lines in the file
for /f %%N in ('find /c /v "" ^<%input%') do set /a end=%%N+1
:break

::extract the section bracketed by begin and end
set /a count=end-begin-1
<%input% (
  rem ::throw away the beginning lines until we reach the desired section
  for /l %%N in (1 1 %begin%) do set /p "ln="
    rem ::read and write the section
    for /l %%N in (1 1 %count%) do (
      set "ln="
      set /p "ln="
      echo(!ln!
    )
)>%output%
setlocal enableDelayedExpansion 设置input=“test.txt” 设置output=“extract.txt” set search=“我的文本” ::查找包含文本的行 对于('findstr/N/c:!search!%input%')中的/f“delims=:”%%N,请设置lineNum=%%N 设置“开始=0” ::查找空行并将“开始”设置为文本前的最后一个空格,将“结束”设置为文本后的第一个空格 对于/f“delims=:”%%N in('findstr/N“^$%input%”)do( 如果%%N lss!lineNum!(设置“开始=%%N”),否则设置“结束=%%N”&转到:中断 ) ::未找到节的结尾,因此我们必须计算文件中的行数
对于/f%%N in('find/c/v”“^下面的程序读取文件行并将一个节的行存储在一个向量中,同时检查搜索文本是否在当前节内。当节结束时,如果找到搜索文本,则输出当前节作为结果;否则,进程将传递到下一节

@echo off
setlocal EnableDelayedExpansion
set infile=input.txt
set outfile=output.txt
set "search=Any text"
set textFound=
call :SearchSection < %infile% > %outfile%
goto :EOF

:SearchSection
   set i=0
   :readNextLine
      set line=
      set /P line=
      if not defined line goto endSection
      set /A i+=1
      set "ln%i%=!line!"
      if not "!ln%i%!" == "!line:%search%=!" set textFound=True
   goto readNextLine
   :endSection
   if %i% == 0 echo Error: Search text not found & exit /B
if not defined textFound goto SearchSection
for /L %%i in (1,1,%i%) do echo !ln%%i!
exit /B
@echo关闭
setlocal EnableDelayedExpansion
设置infle=input.txt
设置outfile=output.txt
设置“搜索=任何文本”
已找到设置文本=
调用:SearchSection<%infile%>%outfile%
后藤:EOF
:搜索部分
集合i=0
:readNextLine
设定线=
设定/付款行=
如果未定义行,则转到endSection
设置/A i+=1
设置“项次%i%=!行!”
如果不是“!ln%i%!”==”!行:%search%=!”则设置textFound=True
转到readNextLine
:结束部分
如果%i%==0回显错误:未找到搜索文本并退出/B
如果未定义textFound转到SearchSection
对于(1,1,%i%)中的/L%%i,执行echo!ln%%i!
退出/B

此程序的限制与dbenham为其程序所述的限制相同。

@StevenSinclair-如果这有效,请不要忘记单击复选标记以选择它作为解决方案。如果您认为此答案有用,则您也可以投票支持该答案(如果您有此倾向)。优雅的解决方案,但我怀疑GOTO循环会使它比我的解决方案慢,尤其是在大文件结尾处找到文本时。我还没有测试。@dbenham:我的方法使用两个而不是GOTO会运行得更快,类似于:
:SearchSection
%(
设置i=0
设置行=
设置/P行=
%。。。