2012-10-23 215 views
0

试图找出如何编写跨多行命令,因为真正的方法是提示(4200个字符),所以我试过这个例子:多行grep命令(不是多行grep)

提出,包含以下三个线测试文件:

some thing
some
thing

当我这样做的grep:
grep "some thing" test

我得到的预期结果:
some thing

但是,当我做到这一点的grep:
grep "some \
thing" test

我得到的意想不到的结果:
some thing
thing

就好像它两次运行grep一次为“一些”和一次为“事物”。有没有什么方法可以正确使用\将2结合到第一个grep的结果?

+1

只需重新阅读这个...什么_exactly_是你的预期输出? – Steve

+0

@steve我希望和单行相同(所以第一个例子,只有'some thing'返回)。 – Hershizer33

回答

0

所以它看起来像这样做的:
grep "some \
|thing" test.txt

制作:some thing

同:grep "some thing" test.txt

3

而不是的grep,你必须使用pcregrep,那么你就可以通过-M选项以使用模式的新线。

在您的例子将是这样的:

pcregrep -M 'some\nthing' test 
+0

+酷!用-z -o你也可以让grep做到这一点(见下文)。 – JJoao

1

使用-zPo你可以让grep表现多种语言:

grep -zPo 'some\nthing' test