Reputation: 1
I am trying to conduct a search of three conditions joined with AND with grep
I am able to search for b,
AND ghkj
like this:
grep -e "b,.*ghkj"
but I am unable to search for b,
AND ghkj
AND uuuuuu
like this:
grep -e "b,.*ghkj.*uuuuuu"
How do I create a 3 condition AND search with grep
?
Upvotes: 0
Views: 2280
Reputation: 4089
There is no AND operator in grep (or any regex language afaik)
grep -E -e 'aaa.*bbb'
Matches any line that includes 'aaa' followed by any number of characters (.*
) and then 'bbb', this simulates an AND in that it matches lines containing 'aaa' and 'bbb' in that order, but it will not match 'bbbaaa'. To get all lines containing 'aaa' and 'bbb' we can use an OR (|
) to cover all possible cases.
grep -E -e 'aaa.*bbb|bbb.*aaa'
This strategy does not extend well to 3 or more clauses though, since the number of cases grows exponentially.
Instead we can use pipes to achieve an AND.
grep -E -e 'aaa' file | grep -E -e 'bbb' | grep -E -e 'ccc'
file
containing 'aaa' and outputs them. The final output will only be lines from file
which contain 'aaa' AND 'bbb' AND 'ccc' in any order.
Upvotes: 2