如何从多个文件.txt中获取Windows批处理文件中两个特定行之间的行

如何从多个文件.txt中获取Windows批处理文件中两个特定行之间的行

需要从文本文件中提取两行之间的内容。

根据类似问题的研究:
批处理脚本用于提取两行之间的行

制作示例脚本

SETLOCAL EnableDelayedExpansion


for %%F in (*.txt) do (

for /f "tokens=1 delims=[]" %%a in ('find /n "----"^<%%F') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "========="^<%%F') do set /a end=%%a
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<%%F') do (
  if %%a geq !start! if %%a leq !end! echo(%%b
 )

该脚本能够设置变量开始和结束,但即使在我的中,最后
if一条语句也无法对它们进行评估。!!variables!

文本文件的内容:

line   one
line   two ----------------
line three  Needed content
line  four  Needed content
line  five ================
line   six
line seven ----------------
line eight  Needed content
line  nine  Needed content
line   ten ================
line   ...

答案1

  • 对于一个文件:
@echo off && setlocal enabledelayedexpansion

set "_line=0" & for /f "delims=" %%i in ('type "Z:\SU_2022\Q1720596\Some File Text.txt"
    ')do if "!_line!" == "0" (echo;"%%~i"| findstr /c:"----" >nul && set "_line=1" )else (
         echo\"%%~i" | findstr /c:"====" >nul && set "_line=0" || (set /p "'=%%~i" <nul & echo\))
         
endlocal  

  • 我的测试文件的内容:Z:\SU_2022\Q1720596\Some File Text.txt"
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type 
-------------------------------------------------------------------------------
specimen book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was popularised in 
===============================================================================
the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, 
and more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum.

Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots 
-------------------------------------------------------------------------------
in a piece of classical Latin literature from 45 BC, making it over 2000 years old. 
===============================================================================
Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, 
looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum 
passage, and going through the cites of the word in classical literature, discovered 
the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 
de Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, written 
-------------------------------------------------------------------------------
in 45 BC. This book is a treatise on the theory of ethics, very popular during the 
Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes 
from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 
===============================================================================
1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 
from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact 
original form, accompanied by English versions from the 1914 translation by H. Rackham.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content 
of a page when looking at its layout. The point of using Lorem Ipsum is that it has a 
more-or-less normal distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English. Many desktop publishing packages and web page
editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum'
-------------------------------------------------------------------------------
will uncover many web sites still in their infancy. Various versions have evolved over the
===============================================================================
years, sometimes by accident, sometimes on purpose (injected humour and the like).
  • 输出结果呼应:
specimen book. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in
in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
in 45 BC. This book is a treatise on the theory of ethics, very popular during the
Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes
from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the
will uncover many web sites still in their infancy. Various versions have evolved over the

  • 对于多个文件:
@echo off && setlocal enabledelayedexpansion

for %%G in (*.txt)do set "_line=0" & for /f "delims=" %%i in (
    'type "%%~G"')do if "!_line!" == "0" (echo;"%%~i"| findstr /c:"----" >nul && set "_line=1"
        )else (echo\"%%~i" | findstr /c:"====" >nul && set "_line=0" || (set /p "'=%%~i" <nul & echo\))
         
endlocal 

在屏幕上。需要源文本文件名显示为 在同一行字符串输出中的前缀。

@echo off && setlocal enabledelayedexpansion

for %%G in (*.txt)do set /p "'=%%~nxG" <nul & echo/ & set "_line=0" & for /f "delims=" %%i in (
    'type "%%~G"')do if "!_line!" == "0" (echo;"%%~i" | findstr /c:"----" >nul && set "_line=1"
        )else (echo\"%%~i" | findstr /c:"====" >nul && set "_line=0" || (set /p "'=%%~i" <nul & echo\))
         
endlocal 

观察:它有-“ ”的多种变体,尝试复制文件的字符并粘贴:findstr /c:"----"


其他资源:

相关内容