Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用FINDSTR仅获取匹配模式的行号_Windows_Batch File_Cmd_Pattern Matching_Findstr - Fatal编程技术网

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

Windows 如何使用FINDSTR仅获取匹配模式的行号,windows,batch-file,cmd,pattern-matching,findstr,Windows,Batch File,Cmd,Pattern Matching,Findstr,我被困在windows批处理(cmd)模式搜索中。我需要在文件中搜索一个模式,并需要返回行号。我使用了带有/X选项的FINDSTR,但它也将带图案的匹配行附加到行号上 for /f "delims=:" %%a in ('findstr /n "pattern" "file"') do echo "pattern" found in line #%%a 此外,我没有安装任何实用程序(如unix实用程序)的特权,因此我可以使用cut提取行号。endro发布了一个很好的纯批处理解决方案 for /

我被困在windows批处理(cmd)模式搜索中。我需要在文件中搜索一个模式,并需要返回行号。我使用了带有
/X
选项的
FINDSTR
,但它也将带图案的匹配行附加到行号上

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

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

endro发布了一个很好的纯批处理解决方案

for /f "delims=:" %%a in ('findstr /n "pattern" "file"') do echo "pattern" found in line #%%a
另一个选项是使用我编写的名为REPL.BAT的混合JScript/batch实用程序,它对stdin执行正则表达式搜索和替换,并将结果写入stdout。它完全基于脚本,因此不需要安装可执行文件。从XP开始,它可以在任何现代Windows机器上运行

假设REPL.BAT在您当前的目录中,或者更好地说,在您的路径中的某个地方:

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

我不确定它为什么不起作用…>对于('findstr/n/C:'Line 1“test')中的/f“delims=:”%%a,此时在第#%%a%%行中发现了“echo”模式,这是意外的。请看一下…@AbinashBishoyi-如果直接从命令行运行,那么您需要在这两个位置使用
%a
。在批处理脚本中使用时,百分比仅加倍。