如何使用 FINDSTR 仅获取匹配模式的行号

Abi*_*oyi 2 windows cmd batch-file pattern-matching findstr

我被困在 Windows 批处理(cmd)模式搜索中。我需要在文件中搜索模式并需要返回行号。我使用了FINDSTRwith/X选项,但它也将模式匹配的行附加到行号。

此外,我没有安装任何实用程序(如 unix-utilities)的特权,以便我可以cut用来提取行号。

End*_*oro 5

for /f "delims=:" %%a in ('findstr /n "pattern" "file"') do echo "pattern" found in line #%%a
Run Code Online (Sandbox Code Playgroud)

  • @AbinashBishoyi - 如果直接从命令行运行,那么您需要在两个地方都使用 `%a`。在批处理脚本中使用时,百分比只会翻倍。 (2认同)