从现有文本文件中查找和提取文本 [英] Find and extract text from within existing text file

查看:84
本文介绍了从现有文本文件中查找和提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从现有文本文件中提取数据.文本文件的结构看起来像这样……

I need to be able to extract data from within an existing text file. The structure of the text file looks something like this...

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根本不是该应用程序的选择,否则它早就已经结束了.有任何想法吗?谢谢.

As you can see, the text file is arranged in sections. There is always a single header line, followed by a random number of other data lines, and there is always a blank line between sections. Unfortunately, there is no rhyme or reason to the naming scheme of the header sections or the data contained within the other data lines...only the aforementioned structure is somewhat consistent. The data I need to search for is located within one of the other data lines, in only one of the sections, which could be located anywhere within the text file. I can use the FIND command to locate the text I need to find, but once I do that, I need to be able to extract the entire section to a new text file. I can't figure out how to go up however many lines to the first preceeding blank line, then go down to the next following blank line, and extract everything in between. Does that make sense? Unfortunately, VBScript is simply not an option for this application or it would've been over and done with long ago. Any ideas? Thanx.

推荐答案

下面的程序读取文件行并将一个节的行存储在矢量中,同时检查搜索文本是否在当前节内.该部分结束时,如果找到了搜索到的文本,则将当前部分作为结果输出;否则,将显示当前部分.否则,该过程将转到下一部分.

The program below read file lines and store the lines of one section in a vector, at the same time it check if the search text is inside current section. When the section ends, if the searched text was found, current section is output as the result; otherwise, the process pass to the next section.

@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

该程序的局限性与dbenham在其程序中所述的相同.

The limitations of this program are the same that dbenham stated for his program.

这篇关于从现有文本文件中查找和提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆