How to get only line number of the matched pattern using FINDSTR

Abinash Bishoyi picture Abinash Bishoyi · Aug 22, 2013 · Viewed 7.1k times · Source

I got stuck in the windows batch(cmd) pattern search. I need to search for a pattern in a file and need to return the line number. I have used FINDSTR with /X option, but it is also appending the patterned matched line to the line number.

Also I don't have privilege to install any utility like unix-utilities so that I can use cut to extract the line number.

Answer

Endoro picture Endoro · Aug 22, 2013
for /f "delims=:" %%a in ('findstr /n "pattern" "file"') do echo "pattern" found in line #%%a