2

我在windows dos提示符下。我有日志文件,其中包含如下日志:

Timestamp: Order received for Item No. 26551
Timestamp: Exception: OutOfRangeException
Timestamp: Message: Inventory Item is not stock. Item No. 23423
Timestamp: Order received for Item No. 23341

我想提取所有给出某种异常的项目编号。我为此使用 findstr 命令。如何在正则表达式中使用换行符?我想要所有有异常字的行和下一行的项目号。

有什么帮助吗?

4

5 回答 5

5

我发现了一个未记录的功能 - FINDSTR 可以匹配换行符<CR><LF>在后续行中继续匹配。但是搜索字符串必须在命令行中指定,换行符必须在变量中,并且值必须通过延迟扩展传递。

另一个复杂情况是 FOR 循环的 IN() 子句在单独的隐式 CMD 会话中执行,并且必须重新启用延迟扩展。此外,!必须对字符进行转义,以便它们进入第二个 CMD 会话。

这个小测试脚本可以解决问题。

@echo off
setlocal enableDelayedExpansion
if "%~1"==":doSearch" goto :doSearch

::Define a variable as a LineFeed (0x0A) character
set LF=^


:: The above 2 blank lines MUST be preserved!

::Define a CR variable as a CarriageReturn (0x0D) character
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

set file="test.txt"
for /f "delims=" %%A in ('cmd /v:on /c^"findstr /rc:"Item No\. .*^!CR^!*^!LF^!.* Exception: " %file%^"') do (
  set "ln=%%A"
  set "item=!ln:*Item No. =!"
  echo Item No. !item! had an exception
)
exit /b


编辑 2015-01-11

我只是重读了这个问题,并意识到我错了。OP 想要异常字符串出现在前一行的项目编号(查找后搜索),但我的解决方案只能找到异常出现在后续行的项目编号(前瞻搜索)。

不幸的是,没有办法让 FINDSTR 在搜索后进行查看。

在大多数情况下,我会删除上面的答案,因为它没有回答问题。但是,这个答案确实记录了一个以前没有描述过的新颖的 FINDSTR 功能,它可能非常有用。前瞻功能在概念上与后瞻功能足够接近,需要它的人可能会通过这个问题找到答案,所以我打算保留它。

我确实有一个纯粹的基于脚本的解决方案,它可以在从 XP 开始的任何 Windows 机器上运行,但它不使用 FINDSTR。JREPL.BAT是一个正则表达式命令行,可以轻松提取所需的项目编号。

jrepl "Item No\. (\d+)\r\n.* Exception: " $1 /m /jmatch /f test.txt
于 2012-01-07T22:09:36.987 回答
1

我查看了findstr 文档,我认为它无法进行多行搜索。

可能您应该使用更高级的工具,例如 awk,或者某些版本的 grep 似乎也支持多行正则表达式。

你可以看看stackoverflow.com/questions/152708/

于 2011-04-12T07:59:39.507 回答
0

另一个 powershell 示例,具有积极的向后看:

'Timestamp: Order received for Item No. 26551
Timestamp: Exception: OutOfRangeException
Timestamp: Message: Inventory Item is not stock. Item No. 23423
Timestamp: Order received for Item No. 23341
Timestamp: Exception: OutOfRangeException
Timestamp: Message: Inventory Item is not stock. Item No. 23424' | 
  set-content input.txt

get-content -raw input.txt | 
  select-string -allmatches '((?<=exception.*\n.*)\d+)' | 
  % matches | % value
23423
23424
于 2021-09-01T23:23:53.843 回答
-1

如果您使用的是 Vista 或 Windows 7(或者如果您在 XP 上手动安装),则可以使用 PowerShell 来执行此操作:

$resultlist = new-object System.Collections.Specialized.StringCollection
$regex = [regex] '(?m)^.*Exception.*\r\n.*Item No\. (\d+)'
$match = $regex.Match($subject)
while ($match.Success) {
    $resultlist.Add($match.Groups[1].Value) | out-null
    $match = $match.NextMatch()
} 

$resultlist然后将包含所有项目编号的列表,其中包含一行Exception

于 2011-04-12T08:37:48.913 回答
-2

如果您可以下载工具,这里有一个您可以使用的Ruby for Windows命令。

C:\work>ruby -ne "print gets.split.last if /Exception/" file
23423
C:\work>type file
Timestamp: Order received for Item No. 26551
Timestamp: Exception: OutOfRangeException
Timestamp: Message: Inventory Item is not stock. Item No. 23423
Timestamp: Order received for Item No. 23341

C:\work>ruby -ne "print gets.split.last if /Exception/" file
23423
于 2011-04-12T08:52:51.040 回答