[NTLUG:Discuss] in a world where grep doesn't have the -m (match) option..
Leroy Tennison
leroy_tennison at prodigy.net
Tue Mar 25 22:41:37 CDT 2008
Robert Citek wrote:
> On Tue, Mar 25, 2008 at 5:03 PM, Chris Cox <cjcox at acm.org> wrote:
>> Richard wrote:
>> > Chris Cox wrote:
>> >> Richard wrote:
>> >>
>> >>> ..how would one go about stopping a grep after the first match? <snippage>
>> >> <snippage>
>> >>
>> >> grep 'whatever' file | head -1
>> >> (that's a minus one)
>> >>
>> > AH YES!! That's brilliant! It would cause the search to continue..but
>> > WOULD get the job done. Thanks Chris!
>> >
>>
>> Shouldn't continue. Should terminate after a full buffer containing
>> the matched string goes to the head command.
>
> For example:
>
> $ time -p yes hello | grep hello | head -10000000 | wc -l
> 10000000
> real 2.04
> user 2.40
> sys 0.30
>
> $ time -p yes hello | grep hello | head -1 | wc -l
> 1
> real 0.01
> user 0.01
> sys 0.00
>
> $ time -p yes hello | grep -m 1 hello | wc -l
> 1
> real 0.00
> user 0.00
> sys 0.00
>
> Regards,
> - Robert
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
A different approach (admittedly wandering because it's not using grep)
is an awk script:
$0 ~ "whatever" {print $0; exit;}
More information about the Discuss
mailing list