How can I use findstr with newline regular expression

Morin picture Morin · Apr 12, 2011 · Viewed 10.5k times · Source

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

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.

any help?

Answer

dbenham picture dbenham · Jan 7, 2012

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.

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

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).

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

Under most circumstances, I would delete the answer above, since it does not answer the question. However, this answer does document a novel FINDSTR functionality that had not been described before that could be very useful. The look ahead feature is close enough in concept to the look behind feature that someone that needs it might find the answer via this question, so I plan to keep it.

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