Windows批处理脚本 - 查找字符串并回显所有内容

时间:2016-09-21 13:16:45

标签: windows batch-file

我正在创建摘要审核文件,我正在扫描的其中一个文件是300-1000 +页。我试图提取到此审计文件的信息总是最后3页左右。

因此,虽然我对findstr或find函数有点熟悉,但在找到该信息之后我不知道如何返回所有内容。帮助

使用该逻辑,我如何在余额报告后得到所有内容?

FINDSTR /l "BALANCE REPORT" EXAMPLE.REPORT 1>NUL 2>&1 && (
ECHO WHAT DO I PUT HERE??? >> NightlyAudit.txt
) || (
ECHO No Balance Report Found! >> NightlyAudit.txt
)

2 个答案:

答案 0 :(得分:4)

我认为您的FINDSTR搜索可能不正确 - "BALANCE REPORT"会匹配包含BALANCEREPORT的任何行。我怀疑你想要匹配文字字符串BALANCE REPORT,在这种情况下你需要使用/c:"BALANCE REPORT"

您可以添加/ N选项以获取匹配的行号,使用FOR / F解析值,然后使用MORE和+ n跳过适当的行数。

以下将写出匹配行

之后的所有行
@echo off
setlocal
set "skip="
for /f "delims=:" %%N in (
  'findstr /nlc:"BALANCE REPORT" example.report'
) do if not defined skip set skip=%%N
if not defined skip (
  echo BALANCE REPORT not found
  exit /b 1
)
more +%skip% example.report >NightlyAudit.txt

如果要在输出中包含匹配行,则必须减去一行。如果第一行碰巧匹配,那么你不想跳过任何行,但是MORE不允许+0,所以逻辑需要的不仅仅是减去1。

@echo off
setlocal
set "skip="
for /f "delims=:" %%N in (
  'findstr /nlc:"BALANCE REPORT" example.report'
) do if not defined skip set skip=%%N
if not defined skip (
  echo BALANCE REPORT not found
  exit /b 1
)
set /a skip-=1
if %skip% equ 0 (
  set "skip="
) else (
  set "skip=+%skip%"
)
more %skip% example.report >NightlyAudit.txt

请注意,MORE会将任何[Tab]字符转换为一系列空格。还有一个潜在的问题。我不确定MORE可以跳过超过64k行,如果你的源文件有1000页,每行60行,那么你可能会危险地接近极限。

所以有可能MORE对你不起作用,在这种情况下你需要一个替代方案。这是使用纯批处理的一种方法,但它相对较慢,每行的限制为~8190字节。

将以下内容替换为MORE命令以在匹配后打印所有行:

...
>NightlyAudit.txt (
  for /f "skip=%skip% delims=" %%A in ('findstr /n "^" example.report') do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    echo(!ln:*:=!
    endlocal
  )
)

如果要包含匹配的行,则SKIP变量的计算会有所改变,FOR / F选项也是如此。 FOR / F也不允许skip = 0,但语法与MORE有点不同。

...
set /a skip-=1
if %skip% equ 0 (
  set "skip="
) else (
  set "skip=skip=%skip%"
)
>NightlyAudit.txt (
  for /f "%skip% delims=" %%A in ('findstr /n "^" example.report') do (
  ...

如果您使用其他脚本语言,只需读取一次输入即可轻松实现目标。例如,您可以将JScript与CSCRIPT一起使用。

例如,以下内容可用于在匹配后写入所有行:

<强> writeAfterMatch.js

var str, go=0;
while (!WScript.StdIn.AtEndOfStream) {
  str=WScript.StdIn.ReadLine();
  if (go) WScript.StdOut.WriteLine(str);
  if (!go) if (str.indexOf(WScript.Arguments(0))>0) go=1;
}

<强>使用

cscript //nologo writeAfterMatch.js "BALANCE REPORT" <example.report >NightlyAudit.txt

只需颠倒几行的顺序,就可以包含匹配的行:

<强> writeAfterMatchInclusive.js

var str, go=0;
while (!WScript.StdIn.AtEndOfStream) {
  str=WScript.StdIn.ReadLine();
  if (!go) if (str.indexOf(WScript.Arguments(0))>0) go=1;
  if (go) WScript.StdOut.WriteLine(str);
}

<强>使用

cscript //nologo writeAfterMatchInclusive.js "BALANCE REPORT" <example.report >NightlyAudit.txt

另一个选择是使用我的JREPL.BAT text processing utility以最少的代码解决问题。 JREPL是纯脚本(混合JScript /批处理),可以在XP以后的任何Windows机器上本机运行。通过从命令行发出JREPL /??,可以获得完整的文档。

它可能看起来令人生畏,因为有很多选择。但解决方案实际上相当简单。

以下一个班轮将在匹配

后写出所有行
jrepl "BALANCE REPORT" "go++;$0" /l /j /jendln "if(go<2)$txt=false;if(go)go++" /jbeg "go=0" /f example.report /o NightlyAudit.txt

这个更简单的变体将包括匹配行:

jrepl "BALANCE REPORT" "go=1;$0" /l /j /jendln "if(!go)$txt=false" /jbeg "go=0" /f example.report /o NightlyAudit.txt

如果您在自己的批处理脚本中使用JREPL命令,则需要使用CALL JREPL。

答案 1 :(得分:2)

一般结构是:

FINDSTR [switch(es)] [string(s)] [source] 1>NUL 2>&1 && (
    [successful commands]
) || (
    [unsuccessful commands]
)