Question

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.

Was it helpful?

Solution 2

Endoro has posted a good pure batch solution.

Another option is to use a hybrid JScript/batch utility I wrote called REPL.BAT that performs regex search and replace on stdin and writes the result to stdout. It is purely script based, so no executables need to be installed. It works on any modern Windows machine from XP onward. REPL.BAT is available here.

Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH:

findstr /n "pattern" "file.txt"|repl :.* ""

OTHER TIPS

for /f "delims=:" %%a in ('findstr /n "pattern" "file"') do echo "pattern" found in line #%%a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top