35

これは基本ですが、グーグルできません。grepの呼び出しに使用できますか

grep expr1 | grep expr2

expr1とexpr2の両方を含む行を出力するように?

4

4 に答える 4

45

You can provide several expressions to search for with several -e flags. See also this post.

i.e.

grep -e expr1 -e expr2

于 2014-05-09T17:51:49.727 に答える
33

Try this:

grep 'expr1.*expr2\|expr2.*expr1'

That's a little more complicated than it needs to be if you know that "expr2" will always come after "expr1". In that case, you could simplify it to:

grep 'expr1.*expr2'

于 2013-02-27T15:57:49.910 に答える
3

What you have should work.

The following is an alternative way to achieve the same effect:

grep -E 'expr1.*expr2|expr2.*expr1'
于 2013-02-27T15:57:11.860 に答える
1

Actually your own suggestion is nearly correct if you want to get the lines that contain both expressions. Use:

grep expr1 somefile | grep expr2
于 2014-07-07T12:18:07.417 に答える