管道中的 grep:为什么它不起作用

grep in pipeline: why it does not work

我想从程序的输出中提取某些信息。但是我的方法不起作用。我写了一个相当简单的脚本。

#!/usr/bin/env python

print "first hello world."
print "second"

使脚本可执行后,我键入 ./test | grep "first|second"。我希望它能显示这两个句子。但它没有显示任何内容。为什么?

对表达式进行转义。

$ ./test | grep "first\|second"
first hello world.
second

另请记住,shebang 是 #!/usr/bin/env python,而不仅仅是 #/usr/bin/env python

使用\|代替|

./test | grep "first\|second"