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+)
.* Exception: " $1 /m /jmatch /f test.txt
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…