批处理脚本:如何从文件中查找2行

时间:2012-02-10 15:01:08

标签: batch-file

生成了一个日志文件gettting,我必须找出以下行是否有?

日志文件:

ftp>连接到服务器。

FTP> mget xyz *

200类型设置为A.

FTP> mget abc *

200将类型设置为A

FTP>再见

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

FTP>"

请让我知道如何搜索连续的行,在第二行我试图找到一个字符串。

1 个答案:

答案 0 :(得分:1)

我不确定每一行之间是否有空行。下面的解决方案假设每条线之间没有空行。换句话说,下面的解决方案将匹配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?