Batch file that searches cmdline output for keyword and displays the previous line(x5)

  batch-file, command-line, windows

I have a batch file that runs a command in the command line and searches the output for a keyword. It then outputs the line previous to the line with the keyword.

For example: Search for KEYWORD

PREVIOUS LINE

KEYWORD

result: PREVIOUS LINE

The code is as follows

set Output="pnputil -e"
FOR /F "eol=. tokens=*" %%a IN ( '%Output%' ) DO (
    set line=%%a
    set ourline=!line:KEYWORD=!
    if not !line!==!ourline! (
        SET Output=!prev_line!
    )
    SET prev_line=%%a
)
echo Installed OEM file found as: !Output!

I want to replicate this but the line I want is 5 lines previous to the keyword. The output I want is followed by 4 lines of random info with only one constant keyword 5 lines after it that I can search.

Source: Windows Questions

LEAVE A COMMENT