windows 管道 findstr 的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2737107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:22:54  来源:igfitidea点击:

piping findstr's output

windowscommand-linepipefindstr

提问by Gauthier

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

Windows 命令行,我想搜索所有行的文件:

# NNN "<file>.inc"

where NNNis a number and <file>any string.

其中NNN是一个数字和<file>任何字符串。

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

我想使用 findstr,因为我不能要求脚本的用户安装 ack。

Here is the expression I came up with:

这是我想出的表达方式:

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

The file to search is all_pre.txt.

要搜索的文件是all_pre.txt.

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

到现在为止还挺好。现在我想将它传递给另一个命令,例如more.

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

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).

其结果与前一个命令的输出相同,但每行都以文件名作为前缀 (all_pre.txt)。

Then comes:

然后来了:

FINDSTR: cannot open |
FINDSTR: cannot open more

Why doesn't the pipe work?

为什么管道不工作?



snip of the content of all_pre.txt

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)"


EDIT: 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


EDIT after Frank Bollack:

在 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?

不起作用,尽管(我认为)它应该查找与之前相同的字符串,然后任意次数查找任何字符。那必须包括",对吗?

回答by Frank Bollack

You are missing a trailing \"in your search pattern.

\"的搜索模式中缺少尾随。

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

The above works for me.

以上对我有用。

Edit:

编辑:

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)"

Edit:

编辑:

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

为了规避 中的这个“错误” findstr,您可以将搜索放入一个批处理文件中,如下所示:

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

Name it something like myfindstr.batand call it like that:

将它命名为类似的名称并这样myfindstr.bat称呼它:

myfinsdtr all_pre.txt | more

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

您现在可以像往常一样使用管道和重定向运算符。

Hope that helps.

希望有帮助。

回答by intech_guy

I can't really explain the why, but from my experience although findstrbehaviour with fixed strings (e.g. /c:"some string") is exactly as desired, regular expressions are a different beast. I routinely use the fixed string search function like so to extract lines from CSV files:

我无法真正解释原因,但根据我的经验,尽管findstr使用固定字符串(例如/c:"some string")的行为完全符合要求,但正则表达式是一个不同的野兽。我经常使用像这样的固定字符串搜索功能从 CSV 文件中提取行:

C:\> findstr /C:"literal string" filename.csv >  output.csv

No issue there.

没有问题。

But using regular expressions (e.g. /R "^\"some string\"") appears to force the findstroutput to console and can't be redirected via any means. I tried >, >>, 1>, 2>and all fail when using regular expressions. My workaround for this is to use findstr as the secondary command. In my case I did this:

但是使用正则表达式(例如/R "^\"some string\"")似乎会强制findstr输出到控制台并且无法通过任何方式重定向。我试过>, >>, 1>,2>和使用正则表达式时都失败了。我的解决方法是使用 findstr 作为辅助命令。就我而言,我这样做了:

C:\> type filename.csv | findstr /R "^\"some string\"" > output.csv

That worked for me without issue directly from a command line, with a very complex regular expression string. In my case I only had to escape the " for it to work. other characters such as , and . worked fine as literals in the expression without escaping.

直接从命令行对我有用,没有问题,有一个非常复杂的正则表达式字符串。在我的情况下,我只需要转义 " 即可工作。其他字符,例如 , 和 . 在表达式中作为文字可以正常工作而不会转义。

I confirmed that the behaviour is the same on both windows 2008 and Windows 7.

我确认在 Windows 2008 和 Windows 7 上的行为是相同的。

EDIT: Another variant also apparently works:

编辑:另一个变体显然也有效:

C:\> findstr /R "^\"some string\"" < filename.csv > output.csv

it's the same principle as using type, but just using the command line itself to create the pipe.

它的原理与 using 相同type,但只是使用命令行本身来创建管道。

回答by Michael Yutsis

If you use a regex with an even number of double quotes, it works perfectly. But your number of " characters is odd, redirection doesn't work. You can either complete your regex with the second quote (you can use range for this purpose: [\"\"]), or replace your quote character with the dot metacharacter.

如果您使用带有偶数个双引号的正则表达式,它可以完美运行。但是您的 " 字符数是奇数,重定向不起作用。您可以使用第二个引号完成正则表达式(您可以为此使用 range: [\"\"]),或者用点元字符替换您的引号字符。

It looks like a cmd.exe issue, findstr is not guilty.

貌似是cmd.exe问题,findstr无罪。