I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:
grep pattern1 | grep pattern2 | ...
So how to convert it to something like?
grep pattern1 & pattern2 & pattern3
I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.
Don't confuse this question with:
grep "pattern1\|pattern2\|..."
This is an OR multi pattern match.
Best Answer
agrep
can do it with this syntax:With GNU
grep
, when built with PCRE support, you can do:With ast
grep
:(adding
.*
s as<x>&<y>
matches strings that match both<x>
and<y>
exactly,a&b
would never match as there's no such string that can be botha
andb
at the same time).If the patterns don't overlap, you may also be able to do:
The best portable way is probably with
awk
as already mentioned:With
sed
:Please beware that all those will have different regular expression syntax.