简体   繁体   中英

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). 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. Here's a sample input 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:

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. I read the man page and the web documentation, and nothing seemed to fit what I'm trying to do.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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