batch script to print previous and next lines of search string in a text file

后端 未结 3 1737
孤城傲影
孤城傲影 2020-12-30 17:44

I have a batch script which will print the entire line of search string into a text file.

for %%i in (log.txt) do (
FINDSTR /G:pattern.txt %%i >> outpu         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 18:17

    @echo off
    setlocal EnableDelayedExpansion
    rem Assemble the list of line numbers
    set numbers=
    for /F "delims=:" %%a in ('findstr /I /N /C:"error occurred" log.txt') do (
       set /A before=%%a-1, after=%%a+1
       set "numbers=!numbers!!before!: !after!: "
    )
    rem Search for the lines
    (for /F "tokens=1* delims=:" %%a in ('findstr /N "^" log.txt ^| findstr /B "%numbers%"') do echo %%b) > output.txt
    

提交回复
热议问题