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

后端 未结 3 1727
孤城傲影
孤城傲影 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:14

    try this:

    for /f "delims=:" %%a in ('findstr /in "error" log.txt') do set /a found=%%a
    if not defined found (echo "error" not found&goto:eof)
    set /a line1=found-1
    set /a line2=found+1
    for /f "tokens=1*delims=:" %%a in ('findstr /in "^" log.txt') do (
        if %%a==%line1% >>output.txt echo(%%b
        if %%a==%line2% >>output.txt echo(%%b
    )
    

提交回复
热议问题