windows - How can I use findstr with newline regular expression -


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

timestamp: order received item no. 26551 timestamp: exception: outofrangeexception timestamp: message: inventory item not stock. item no. 23423 timestamp: order received item no. 23341 

i want extract item number has give sort of exception. i'm using findstr command this. how can use newline in regular expression? want lines have exception word , next line item no.

any help?

i've discovered undocumented feature - findstr can match new line characters <cr> , <lf> , continue match on subsequent lines. search string must specified on command line, new line characters must in variables, , values must passed via delayed expansion.

another complication in() clause of loop executed in separate implicit cmd session, , delayed expansion must re-enabled. also, ! characters must escaped make through 2nd cmd session.

this little test script trick.

@echo off setlocal enabledelayedexpansion if "%~1"==":dosearch" goto :dosearch  ::define variable linefeed (0x0a) character set lf=^   :: above 2 blank lines must preserved!  ::define cr variable carriagereturn (0x0d) character /f %%a in ('copy /z "%~dpf0" nul') set "cr=%%a"  set file="test.txt" /f "delims=" %%a in ('cmd /v:on /c^"findstr /rc:"item no\. .*^!cr^!*^!lf^!.* exception: " %file%^"') (   set "ln=%%a"   set "item=!ln:*item no. =!"   echo item no. !item! had exception ) exit /b 


edit 2015-01-11

i reread question, , realize got wrong. op wanted item number exception string appears on prior line (look behind search), solution can find item number exception appears on subsequent line (look ahead search).

unfortunately, there no way findstr behind search.

under circumstances, delete answer above, since not answer question. however, answer document novel findstr functionality had not been described before useful. ahead feature close enough in concept behind feature needs might find answer via question, plan keep it.

i have purely script based solution runs on windows machine xp onward, not use findstr. jrepl.bat regular expression command line can extract desired item numbers.

jrepl "item no\. (\d+)\r\n.* exception: " $1 /m /jmatch /f test.txt 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -