管道findstr的输出 [英] piping findstr's output

查看:1710
本文介绍了管道findstr的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows命令行,我要搜索一个文件,以所有行开头:

Windows command line, I want to search a file for all rows starting with:

# NNN "<file>.inc"

其中 NNN < file> 任何字符串。

我想使用findstr,因为我不能要求用户

I want to use findstr, because I cannot require that the users of the script install ack.

这是我想出的表达式:

>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*.inc" all_pre.txt

要搜索的文件是 all_pre.txt

到目前为止好了,现在我想将它转换为另一个命令,例如更多

So far so good. Now I want to pipe that to another command, say for example more.

>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*.inc" all_pre.txt | more

这样做的结果与上一个命令的输出相同,但文件名为prefix

The result of this is the same output as the previous command, but with the file name as prefix for every row (all_pre.txt).

然后来:

FINDSTR: cannot open |
FINDSTR: cannot open more

为什么管道不工作?

剪辑all_pre.txt的内容

snip of the content of all_pre.txt

# 1 "main.ss"
# 7 "main.ss"
# 11 "main.ss"
# 52 "main.ss"
# 1 "Build_flags.inc"
# 7 "Build_flags.inc"
# 11 "Build_flags.inc"    
# 20 "Build_flags.inc"
# 45 "Build_flags.inc(function a called from b)"






以逃避正则表达式中的点。不是问题,但值得一提。


I need to escape the dot in the regex also. Not the issue, but worth to mention.

>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc" all_pre.txt






编辑:Frank Bollack:


EDIT after Frank Bollack:

>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc.*" all_pre.txt | more

不工作,虽然(我认为)它应该寻找相同的字符串,字符任意多次。必须包括,对吗?

is not working, although (I think) it should look for the same string as before then any character any number of times. That must include the ", right?

推荐答案

findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*.inc\"" all_pre.txt | more

以上为我工作。

编辑:

findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*\.inc.*\"" all_pre.txt | more

此更新的搜索字符串现在将匹配您示例中的这些行:

This updated search string will now match these lines from your example:

# 1 "Build_flags.inc"
# 7 "Build_flags.inc"
# 11 "Build_flags.inc"
# 20 "Build_flags.inc"
# 45 "Build_flags.inc(function a called from b)"


$ b b

编辑

为了避免 findstr 您可以将您的搜索放入如下的批处理文件中:

To circumvent this "bug" in findstr, you can put your search into a batch file like this:

@findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc" %1


b $ b

命名为 myfindstr.bat ,并像这样调用:

myfinsdtr all_pre.txt | more

现在可以使用管道和重定向运算符

You can now use the pipe and redirection operators as usual.

希望有帮助。

这篇关于管道findstr的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆