You cannot transform the filter
grep a | grep c | grep -v d to a single simple
grep. There are only complicated and ineffective ways. The result has slow performance and the meaning of the expression is obscured.
Single command combination of the three greps
If you just want to run a single command you can use
awk which works with regular expressions too and can combine them with logical operators. Here is the equivalent of your filter:
awk '/a/ && /c/ && $0 !~ /d/'
I think in most cases there is no reason for simplifying a pipe to a single command except when the combination results in a realatively simple grep expression which could be faster (see results below).
Unix-like systems are designed to use pipes and to connect various utilities together. Though the pipe communication is not the most effective possible but in most cases it is sufficient. Because nowadays most of new computers have multiple CPU cores you can "naturally" utilize CPU parallelization just by using a pipe!
Your original filter works very well and I think that in many cases the
awk solution would be a little bit slower even on a single core.
Using a simple program I have generated a random testing file with 200 000 000 lines, each with 4 characters as a random combination from characters
d. The file has 1 GB. During the tests it was completely loaded in the cache so no disk operations affected the performance measurement. The tests were run on Intel dual core.
$ time ( grep -E '^[^d]*a[^d]*c[^d]*$|^[^d]*c[^d]*a[^d]*$' testfile >/dev/null )
$ time ( awk '/a/ && /c/ && $0 !~ /d/' testfile >/dev/null )
The original three greps piped
$ time ( grep a testfile | grep c | grep -v d >/dev/null )
Hybrid - positive greps combined, negative piped
$ time ( grep -E 'a.*c|c.*a' testfile | grep -v d >/dev/null )
Here you see that the single
grep is very slow because of the complex expression. The original pipe of three greps is pretty fast because of a good parallelization. Without parallelization - on a single core - the original pipe runs just slightly faster than
awk which as a single process is not parallelized. Awk and grep probably use the same regular expressions code and the logic of the two solutions is similar.
The clear winner is the hybring combining two positive greps and leaving the negative one in the pipe. It seems that the regular expression with
| has no performance penalty.