管道查找findstr的输入 [英] Piping to findstr's input
问题描述
我有一个文本文件,其中列出了宏名称(每行一个).我的最终目标是打印出宏名称在当前目录的文件中出现多少次.
I have a text file with a list of macro names (one per line). My final goal is to get a print of how many times the macro's name appears in the files of the current directory.
宏的名称在C:\temp\macros.txt
中.
type C:\temp\macros.txt
可以正确打印列表.
现在,我想将输出输出到findstr
的标准输入.
Now I want to pipe that output to the standard input of findstr
.
type C:\temp\macros.txt | findstr *.ss
(ss是我要在其中查找宏名称的文件类型).
type C:\temp\macros.txt | findstr *.ss
(ss is the file type where I am looking for the macro names).
这似乎不起作用,我没有任何结果(非常快,它似乎根本没有尝试). findstr <the first row of the macro list> *.ss
确实有效.
This does not seem to work, I get no result (very fast, it does not seem to try at all). findstr <the first row of the macro list> *.ss
does work.
我也尝试了findstr *.ss < c:\temp\macros.txt
,但没有成功.
I also tried findstr *.ss < c:\temp\macros.txt
with no success.
推荐答案
我认为您对findstr
的工作方式有些困惑.它从文件名(模式)或从stdin获取输入(在其中查找内容-而不是要查找的内容),但是您一直在命令行上输入 来查找所需的内容作为findstr
的参数.
I think you're confusing a little how findstr
works. It gets input (to find things in – not the things to look for) either as a file name (pattern) or from stdin, but the things you're looking for are always given on the command line as an argument to findstr
.
findstr foo xyz.txt
在文件xyz.txt
中找到字符串foo
.
type meh.txt | findstr x
在上一条命令的输出中找到字符串x
(在这种情况下,文件meh.txt
的内容–浪费了type
命令,与常见的cat
误用类似).
finds the string x
in the output of the previous command (in this case the contents of the file meh.txt
– a nice waste of the type
command, much akin to common misuse of cat
).
由于您追求的是 counts 而不是宏名称出现的实际行,因此我建议采用另一种方法.假设您的包含宏的文件每行列出一个宏:
Since you're after the counts instead of the actual lines the macro names appear in, I'd suggest a different approach. This assumes that your file containing the macros lists them one per line:
for /f "delims=" %x in (macros.txt) do @(echo %x: & find /c "%x" *.ss)
for
循环逐行遍历文件的内容.然后,它会继续打印您要搜索的名称,并执行find /c
,它实际上会计算匹配的行数.
The for
loop iterates over the contents of your file, line-wise. It then proceeds to print the name you're searching for and executing find /c
which actually counts matching lines.
这篇关于管道查找findstr的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!