通过管道连接到 findstr 的输入
我有一个文本文件,其中包含宏名称列表(每行一个)。我的最终目标是打印宏名称在当前目录的文件中出现的次数。
宏的名称位于 C:\temp\macros.txt
中。
在命令提示符中输入 C:\temp\macros.txt 可以正常打印列表。
现在我想将该输出通过管道传输到 findstr
的标准输入。
类型 C:\temp\macros.txt | findstr *.ss
(ss 是我在其中查找宏名称的文件类型)。
这似乎不起作用,我没有得到任何结果(非常快,它似乎根本没有尝试)。 findstr <宏列表第一行> *.ss
确实有效。
我还尝试了 findstr *.ss
findstr *.ss
findstr *.ss
findstr *.ss < c:\temp\macros.txt
没有成功。
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.
The macro's names are in C:\temp\macros.txt
.
type C:\temp\macros.txt
in the command prompt prints the list alright.
Now I want to pipe that output to the standard input of findstr
.
type C:\temp\macros.txt | findstr *.ss
(ss is the file type where I am looking for the macro names).
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.
I also tried findstr *.ss < c:\temp\macros.txt
with no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您对
findstr
的工作原理有些困惑。它以文件名(模式)或标准输入的形式获取输入(以查找其中的内容,而不是要查找的内容),但您要查找的内容总是在命令行上给出作为findstr
的参数。在文件
xyz.txt
中查找字符串foo
。在上一个命令的输出中查找字符串
x
(在本例中为文件meh.txt
的内容) – 浪费了type
命令,非常类似于常见的cat
误用)。由于您关注的是计数,而不是宏名称出现的实际行,因此我建议采用不同的方法。这假设包含宏的文件每行列出一个宏:
for
循环逐行迭代文件的内容。然后它会继续打印您正在搜索的名称并执行find /c
,这实际上会计算匹配的行数。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 tofindstr
.finds the string
foo
in the filexyz.txt
.finds the string
x
in the output of the previous command (in this case the contents of the filemeh.txt
– a nice waste of thetype
command, much akin to common misuse ofcat
).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:
The
for
loop iterates over the contents of your file, line-wise. It then proceeds to print the name you're searching for and executingfind /c
which actually counts matching lines.