windows 使用 findstr 的类似 grep 的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8473425/
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 18:38:05  来源:igfitidea点击:

grep like syntax with findstr

windowslinuxgrepwildcardfindstr

提问by bobbyrne01

ive been using grep a lot on linux lately but now i need to use findstr to carry out the same tasks on a windows machine and cant quite get the syntax just right.

我最近在 linux 上经常使用 grep,但现在我需要使用 findstr 在 Windows 机器上执行相同的任务,但不能完全正确地使用语法。

My grep command looks like:

我的 grep 命令看起来像:

grep "12/12/2011.\*followed by literal string" /myFile.txt

so this searches for the date and the literal string specified but can contain a mix of any other characters in between the two search terms by using .\*

所以这将搜索指定的日期和文字字符串,但可以通过使用在两个搜索词之间包含任何其他字符的混合 .\*

Anyone know how to convert this statement to findstr? thanks

任何人都知道如何将此语句转换为findstr?谢谢

回答by Frédéric Hamidi

The findstrcommand supports the /Roption to specify the search string as a regular expression. It's the default behavior, however, so you don't actually need to specify it:

FINDSTR命令支持/R以指定搜索字符串作为一个正则表达式的选项。但是,这是默认行为,因此您实际上不需要指定它:

findstr "12/12/2011.*followed by literal string" myFile.txt

The above should give the same results as your grepexample.

以上应该给出与您的grep示例相同的结果。

回答by Adam Smith

In order to match a literal string, you should use the /Cflag, e.g: /C:"Your literal string here"Otherwise a space inside your regex is counted as an ORe.g. this:

为了匹配文字字符串,您应该使用/C标志,例如:/C:"Your literal string here"否则您的正则表达式中的空格被视为OR例如:

findstr "12/12/2011.*followed by literal string" myFile.txt

is the same as

是相同的

grep 12/12/2011.*followed|by|literal|string myFile.txt

In order to combine both, pipe the output of one into the other, like this:

为了将两者结合起来,将一个的输出通过管道传输到另一个,如下所示:

findstr "12/12/2011.*" myFile.txt | findstr /C:"followed by literal text"