47 points · by mtdewcmu · 10y ago · 45 comments
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.
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.
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.