[NTLUG:Discuss] in a world where grep doesn't have the -m (match) option..

Robert Citek robert.citek at gmail.com
Wed Mar 26 07:10:40 CDT 2008


On Wed, Mar 26, 2008 at 6:49 AM, Rick Renshaw <bofh69 at yahoo.com> wrote:
> --- Steve Baker <steve at sjbaker.org> wrote:
>  > As soon as 'head' has read enough input to satisfy your demand to
>  > display one line, it'll exit.  That causes a 'broken' link on the output
>  > of 'grep' which will crap out and die as a result.  So the only wasted
>  > time is the time grep spends searching onwards while head displays your
>  > line of data.
>
>  If you're searching for a string that only occurs a few times, specifically; less than a buffer
>  full of output, then the grep will complete before head gets the less than full buffer to process.
>   So if you're just searching for the first hit on something that occurs a lot, this will save
>  time.  If you're searching for a string that only occurs 2-3 times, this probably won't save much time.

For example, this ends:

$ time -p { echo hello ; yes bye ; } | grep -m 1 hello | wc -l
1
real 0.02
user 0.01
sys 0.00

This never ends:

$ time -p { echo hello ; yes bye ; } | grep hello | head -1 | wc -l

Regards,
- Robert



More information about the Discuss mailing list