【问题标题】:Batch Script : How to find 2 lines from a file批处理脚本:如何从文件中查找 2 行
【发布时间】:2012-02-10 15:01:08
【问题描述】:

有一个日志文件正在生成,我必须找出以下几行是否没有?

日志文件:

ftp>连接到服务器。

ftp> 管理 xyz*

200 类型设置为 A。

ftp> mget abc*

200 将类型设置为 A

ftp>再见

我必须找出上面的日志文件是否有: "200 类型设置为 A。

ftp>"

请告诉我如何搜索连续的行,在第二行中我试图只找到一个字符串。

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    我不确定每行之间是否有空行。下面的解决方案假设每行之间没有空行。换句话说,下面的解决方案将匹配 2 行,并且第 2 行必须至少有一个字符。

    @echo off
    setlocal disableDelayedExpansion
    ::Define LF variable containing a linefeed (0x0A)
    set LF=^
    
    
    ::Above 2 blank lines are critical - do not remove
    
    ::Define CR variable containing a carriage return (0x0D)
    for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
    
    ::The above variables should be accessed using delayed expansion
    setlocal enableDelayedExpansion
    
    ::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
    findstr /rc:"!CR!*!LF!200 Type set to A!CR!*!LF!." log.txt >nul && (echo found) || echo not found
    

    如果您尝试匹配 3 行中间有一个空行,那么最后一行需要更改为

    findstr /rc:"!CR!*!LF!200 Type set to A!CR!*!LF!!CR!*!LF!." log.txt >nul && (echo found) || echo not found
    

    有关跨换行符搜索以及其他出色的 FINDSTR 功能(和错误!)的更多信息,请参阅What are the undocumented features and limitations of the Windows FINDSTR command?

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      相关资源
      最近更新 更多