简体   繁体   English

grep命令行,带有“或”来自dos窗口(gnu grep)

[英]grep command-line with “or” from dos window (gnu grep)

I'm trying to create a .bat file for windows7, using GNU Grep (grep compiled for Win32 target). 我正在尝试使用GNU Grep(为Win32目标编译的grep)为windows7创建一个.bat文件。 I'm having trouble with the syntax for an expression that uses the | 我在使用|的表达式的语法时遇到问题 (or), as the DOS command thinks I'm using it as a pipe/redirect. (或),因为DOS命令认为我正在使用它作为管道/重定向。 Here's a sample input foo.txt: 这是一个示例输入foo.txt:

aaa
bbb
ccc
aaa bbb ccc

So I try an easy test: 所以我尝试了一个简单的测试:

grep aaa foo.txt

results: 结果:

aaa
aaa bbb ccc

So far, so good. 到现在为止还挺好。 But I really need to extract aaa OR bbb, so I use this expression: (aaa|bbb) ex: 但我真的需要提取aaa OR bbb,所以我使用这个表达式:(aaa | bbb)ex:

grep (aaa|bbb)

gives me: 给我:

'bbb)' is not recognized as an internal or external command,
operable program or batch file.

I've tried various quotes, but haven't hit the right combination yet. 我尝试了各种报价,但尚未找到合适的组合。 I need to protect the string from the DOS interpreter, yet have it still be useful to grep. 我需要保护字符串不受DOS解释器的影响,但仍然对grep有用。 I read the man page and the web documentation, and nothing seemed to fit what I'm trying to do. 我阅读了手册页和Web文档,似乎没有什么适合我正在尝试做的事情。

Does anyone know how to get this done? 有谁知道怎么做到这一点? thanks. 谢谢。

Need quotes and escaped the pipe with \\ : 需要引号并使用\\转义管道:

grep "aaa\|bbb" foo.txt

$ cat foo.txt
aaa
bbb
ccc
aaa bbb ccc

$ grep "aaa\|bbb" foo.txt
aaa
bbb
aaa bbb ccc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM