批处理文件-找到两行,然后在这两行之间复制所有内容 [英] Batch File - Find two lines then copy everything between those lines

查看:95
本文介绍了批处理文件-找到两行,然后在这两行之间复制所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个文本文件. 我想在文本文件中找到第一行

I need to parse a text file. I want to find the firstline in the text file

: the first line to find
set firstLine=------------------------------------------------------------------------------------------------------------------

并找到最后一行

:: the last line to find
set lastLine=*******************************************************************************************************************

然后我需要将这两行之间的所有内容导出到新文件.

Then I need to export to a new file everything between those two line.

echo     >> M:\TESTING\Output.txt

我是这个的初学者,已经搜寻了好几天,但找不到如何执行此操作.

I'm a beginner with this and I've searched for days, but am not finding how to do this.

我查看了循环和if语句,但仍然感到困惑.

I looked at for loops and if statements, but I'm still puzzled.

    for /f "tokens=1 delims= " %%f in (M:\TESTING\*.txt) do (

    :: sets then the line variable to the line just read
    set line=%%f
    :: the first line to find
    set firstLine=------------------------------------------------------------------------------------------------------------------ 
    :: the last line to find
    set lastLine=*******************************************************************************************************************

Then if %line% = %fistLine% start the export.....

任何方向将不胜感激.谢谢.

Any direction will be appreciated. thanks.

推荐答案

@DennisvanGils的方法是一个很好的开始,在许多情况下都可以胜任.
但是,它不会在给定的行之间生成文本文件内容的精确副本:

@DennisvanGils' approach is a good start and will do well in many cases.
However, it will not produce an exact copy of the text file content between the given lines:

  • 前导空格( SPACE TAB )将被删除(由于tokens=*选项)
  • ;开头的行将被跳过(由于for /F的默认选项eol=;),并且
  • 空行也将被跳过(因为for /F总是会跳过这样的行).
  • leading whitespaces (SPACE and TAB) will be removed (due to tokens=* option),
  • lines starting with ; will be skipped (due to the default option eol=; of for /F), and
  • empty lines will be skipped as well (as for /F always skips such).

要获取文本文件部分的准确副本,可以使用以下代码段:

To get an exact copy of the text file portion, you could use the following code snippet:

@echo off
set "INFILE=M:\TESTING\input.txt"
set "OUTFILE=M:\TESTING\output.txt"
set "FIRSTLINE=------------------------------------------------------------------------------------------------------------------"
set "LASTLINE=*******************************************************************************************************************"
setlocal EnableExtensions DisableDelayedExpansion
set "FLAG="
> "%OUTFILE%" (
    for /F "delims=" %%L in ('findstr /N "^" "%INFILE%"') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*:=!"
        if "!LINE!"=="%FIRSTLINE%" (
            endlocal
            set "FLAG=TRUE"
        ) else if "!LINE!"=="%LASTLINE%" (
            endlocal
            goto :CONTINUE
        ) else if defined FLAG (
            echo(!LINE!
            endlocal
        ) else (
            endlocal
        )
    )
)
:CONTINUE
endlocal

这里的核心功能是findstr,它被配置为返回文件中的每一行,并以行号和冒号:为前缀.然后通过for /F循环解析其输出.由于存在前缀,因此没有一行显示为空,因此循环迭代了每一行.在循环的主体中,通过set "LINE=!LINE:*:=!"命令为每行删除前缀.

Core function here is findstr, which is configured so that every line in the file is returned, prefixed with a line number and a colon :. Its output is then parsed by a for /F loop. Because of the prefix, no line appears to be empty and therefore every one is iterated by the loop. In the body of the loop, the prefix is removed by the set "LINE=!LINE:*:=!" command for each line.

变量FLAG用于决定是否要输出当前行.如果已定义,即已分配值,则执行命令echo !LINE!;否则,不是.如果当前行与%FIRSTLINE%中的字符串匹配,则设置FLAG;否则,将设置FLAG.如果该行与%LASTLINE%中的字符串匹配,则会执行goto命令,从而中断循环.这也意味着仅输出%FIRSTLINE%%LASTLINE%匹配之间的第一个块.

The variable FLAG is used to decide whether or not the current line is to be output; if it is defined, that is, a value is assigned, the command echo !LINE! is executed; otherwise it is not. FLAG is set if the current line matches the string in %FIRSTLINE%; if the line matches the string in %LASTLINE%, a goto command is executed which breaks the loop. This means also that only the first block between %FIRSTLINE% and %LASTLINE% matches is output.

如果文本文件中可能出现多个%FIRSTLINE%%LASTLINE%匹配项,并且您想输出每个块,请用set "FLAG="替换goto命令行.

If there might occur multiple %FIRSTLINE% and %LASTLINE% matches within the text file and you want to output every block, replace the goto command line by set "FLAG=".

请注意,此方法不检查%FIRSTLINE%是否在%LASTLINE%之前出现,甚至不检查%LASTLINE%的存在(以防万一,输出到文件末尾的所有剩余行).如果所有这些都很重要,则需要改进逻辑,甚至很有可能需要第二个循环.

Note that this approach does not check whether %FIRSTLINE% occurs before %LASTLINE%, nor does it even check for existence of %LASTLINE% (all remaining lines to the end of file are output in case). If all this is important, the logic need to be improved and even a second loop will be required most likely.

这篇关于批处理文件-找到两行,然后在这两行之间复制所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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