我如何在换行符正则表达式中使用findstr

我如何在换行符正则表达式中使用findstr

本文介绍了我如何在换行符正则表达式中使用findstr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows dos提示符下。我有一个包含如下日志的日志文件:

I'm on windows dos prompt. I have log file which contains log like:

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命令。如何在正则表达式中使用换行符?我要所有具有例外词的行,然后从下一行开始输入项目编号。

I want to extract all the item number who has give some sort of exception. I'm using findstr command for this. how can I use newline in my regular expression? I want to all lines which have Exception word and from next line the item no.

有帮助吗?

推荐答案

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

I've discovered an undocumented feature - FINDSTR CAN match new line characters <CR> and <LF> and continue the match on subsequent lines. But the search string must be specified on the command line, the new line characters must be in variables, and the values must be passed via delayed expansion.

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

Another complication is the IN() clause of a FOR loop is executed in a separate implicit CMD session, and the delayed expansion must be re-enabled. Also, the ! characters must be escaped so that they make it through to the 2nd CMD session.

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

This little test script does the trick.

@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


EDIT 2015-01-11


EDIT 2015-01-11

我只是重新读了一个问题,意识到我弄错了。 OP希望在上一行显示异常字符串的项目编号(在搜索后查找),但是我的解决方案只能在在下一行显示异常的项目编号(在搜索前查找)。

I just reread the question, and realize I got it wrong. The OP wanted the Item Number where the Exception string appears on the prior line (look behind search), but my solution can only find the Item Number where Exception appears on the subsequent line (look ahead search).

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

Unfortunately, there is no way to get FINDSTR to do a look behind search.

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

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

I do have a purely script based solution that runs on any Windows machine from XP onward, but it does not use FINDSTR. JREPL.BAT is a regular expression command line that can easily extract the desired Item Numbers.

jrepl "Item No\. (\d+)\r\n.* Exception: " $1 /m /jmatch /f test.txt

这篇关于我如何在换行符正则表达式中使用findstr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:20