look(1): lines beginning with given string
linux.die.net
linux.die.net
rev, vipe, comm, bfr, xxd
look uses a linear search by default instead of a binary search, which is what most other implementations use by default.
"look(1): binary search for lines with a given prefix in a sorted file"
Which tells you exactly what the pros/cons are compared to plain grep.
% du -h a
1.1G a
% time look 'dog' a | wc -l
53856
real 0m0.021s
user 0m0.020s
sys 0m0.003s
% time grep '^dog' a | wc -l
53856
real 0m28.593s
user 0m0.977s
sys 0m2.223s
Ok grep performed worse than what I expected (sort took a long time though).Ignoring memory hierarchy, naturally 'look' would be O(log n) while grep would be O(n).
Special input (sorted in this case) of course often calls for matching algorithms, when speed matters.
As for grep itself, it has had many incarnations over the decades with various pluses and minuses.
The current GNU grep allows 3 kinds of regex, basic/extended/perl -- the latter being what ack supports.
Note that Perl regexes have extensions beyond regular languages that are inherently slower than the automata specified by basic regexes. Power versus speed.
E.g. grep(1): "Back-references are very slow, and may require exponential time."
For further info:
> why GNU grep is fast > Mike Haertel mike at ducky.net > Sat Aug 21 03:00:30 UTC 2010
> Here's a blog post from 2006 about a developer trying to "beat grep" and looking at the algorithms it uses; it goes into a little more detail about the "doesn't need to do the loop exit test at every step" optimization mentioned in this email.
http://ridiculousfish.com/blog/posts/old-age-and-treachery.h...
via
https://lists.freebsd.org/pipermail/freebsd-current/2010-Aug...
The best writeup is surely by the inimitable Russ Cox, who really really explains clearly when grep as of 2007 was one of the only fast regex implementations:
Regular Expression Matching Can Be Simple And Fast [#1] (but is slow in Java, Perl, PHP, Python, Ruby, ...)
Russ Cox 2007 jan
http://swtch.com/~rsc/regexp/regexp1.html
(This is a 4 part series but IIRC part 1 has the highlights)
I'm sure that various other tools have been strongly influenced by this famous essay, and so many more things may be as fast as grep by now, but still...
P.S. one of the other high profile "ack"-like search tools would be "ag", aka "The Silver Surfer".
"The Silver Searcher is a 3-5x faster drop in replacement for ack (which itself is better than grep)."
2013
https://www.reddit.com/r/programming/comments/16bvah/the_sil...
look "#" .zshrc
There are really a lot more comments (lines starting with '#') in my .zshrc then the first line.
> As look performs a binary search, the lines in file must be sorted...
I assume you didn't sort your source file first.
sort ~/.zshrc -o /tmp/.zshrc
look "#" /tmp/.zshrc
Sorry for the noise but I am confused... :|
Try setting LC_COLLATE=C and export it and retry.
Thanks for this answer...
look ac | grep date
look -b a |wc -l => 1228
look -bf a |wc -l => 1228
You must sort the /usr/share/dict/words file according to ignore case for the option bf to work properly.
Edit: I was expecting
sort -f /usr/share/dict/words | look -bf a | wc -l to be 5985 but the result is 1228, don't know why.
grep '^string' file.txt
but there's a greater chance of having look installed than grep.I also think your constant time claim sounds too strong. You can eat a lot of end of line search time after the binary search and still beat a linear/regex search.
As someone else pointed out, look can also exit sooner.