Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I doubt you'll get a more efficient way than the one you've selected. Given that the grep executable will already be mapped into memory when the second copy runs, and that you have no backtracking in your regex (unlike an <code>egrep 'foo.*bar|bar.*foo'</code> obvious solution), I think what you have is a fast as you're going to get.</p> <p>Here's some sample timings to illustrate the point:</p> <pre><code>allan@allan-desktop:~$ time egrep 'foo.*bar|bar.*foo' foobar | wc -l 2000 real 0m0.006s user 0m0.004s sys 0m0.004s allan@allan-desktop:~$ time fgrep 'foo' foobar | fgrep 'bar' | wc -l 2000 real 0m0.039s user 0m0.000s sys 0m0.000s allan@allan-desktop:~$ time egrep 'foo.*bar|bar.*foo' foobar | wc -l 2000 real 0m0.006s user 0m0.004s sys 0m0.008s allan@allan-desktop:~$ time fgrep 'foo' foobar | fgrep 'bar' | wc -l 2000 real 0m0.005s user 0m0.004s sys 0m0.004s </code></pre> <p>From this admittedly small sample, the pipeline version takes less system and user CPU time, hence is more efficient.</p> <p>The input file consists of 1000 copies of:</p> <pre><code>foo-bar bar-dgfjhdgjhdgdfgdjghdjghdfg-foo </code></pre> <p>so you can run your own tests.</p> <p>Her's the same test with 100,000 lines of input - you can see the questioners method is more efficient:</p> <pre><code>allan@allan-desktop:~$ time egrep 'foo.*bar|bar.*foo' foobar | wc -l 100000 real 0m0.135s user 0m0.136s sys 0m0.012s allan@allan-desktop:~$ time fgrep 'foo' foobar | fgrep 'bar' | wc -l 100000 real 0m0.034s user 0m0.048s sys 0m0.012s allan@allan-desktop:~$ time egrep 'foo.*bar|bar.*foo' foobar | wc -l 100000 real 0m0.151s user 0m0.144s sys 0m0.000s allan@allan-desktop:~$ time fgrep 'foo' foobar | fgrep 'bar' | wc -l 100000 real 0m0.046s user 0m0.044s sys 0m0.012s </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload