I have some *.bat
file containing find
command to extract some particular line.
for example, if my input text file contains something like:
Login time : XX:XX
username - XXXXXX
Login time : YY:YY
username - YYYYYYY
using username lest say:
find /I "XXXXXX" input.txt | find /I "XXXXXX" > output.txt
I am able to get the username but not sure how to get the correct login time for only the searched user name?
find
(and findstr
) can't process line feeds. They handle each line on its own. So you have to write a script that remembers the last line, checks the current line for the searchstring and prints both the last line and the current line, if the searchstring is found.
I used findstr
instead of find
, because it's more secure (find "XXXXXX"
would also find XXXXXXY
). See findstr /?
for the switches i
, x
and c
.
@echo off
setlocal enabledelayedexpansion
set "search=xxxxxx"
for /f "delims=" %%a in (t.txt) do (
echo %%a|findstr /ixc:"username - %search%" >nul && echo !lastline! %%a
set "lastline=%%a"
)