Re:look(1): lines beginning with given string
by vortico on Saturday December 12, 2015 @10:52PM
(#10725360)
If you don't have this basic linux utility, you can use
but there's a greater chance of having look installed than grep.
[ Parent
| on HN ]
Re:look(1): lines beginning with given string
by DiabloD3 on Saturday December 12, 2015 @11:04PM
(#10725387)
As long as I have been using *nixes, I have never met a machine that didn't have grep. It was written by Ken Thompson himself in 1974, or 41 years ago, and first came out in Unix 4th edition.
Re:look(1): lines beginning with given string
by userbinator on Sunday December 13, 2015 @01:36AM
(#10725607)
grep is also specified by POSIX, whereas look isn't.
Re:look(1): lines beginning with given string
by mtdewcmu on Saturday December 12, 2015 @11:08PM
(#10725392)
The interesting thing about look is that it uses binary search, so it is much faster.
Re:look(1): lines beginning with given string
by discardorama on Sunday December 13, 2015 @12:20AM
(#10725498)
Binary search is useless unless the file is sorted. Most often my files are not sorted, so egrep '^line' works just fine.
Re:look(1): lines beginning with given string
by alayne on Sunday December 13, 2015 @02:27AM
(#10725691)
It obvious that look was written as an optimization for this specific case though. The default file is the user dictionary which is interesting. Do you think they didn't know about grep in 1979?
Re:look(1): lines beginning with given string
by mtdewcmu on Sunday December 13, 2015 @12:38AM
(#10725524)
The interesting use case for me is that you can make a sorted index and look up records efficiently, i.e. within a script. It works with ctags indexes, for instance.
Re:look(1): lines beginning with given string
by userbinator on Sunday December 13, 2015 @01:58AM
(#10725649)
Binary search is only faster if access to the elements is constant-time, which is not true for a file with varying-length records (lines). Line 1000 (counting from 0) could be at offset 2000 if each line is a single character + newline, or it could be at offset 2000000 if each line is 999 characters and a newline, or somewhere in between.
Re:look(1): lines beginning with given string
by mtdewcmu on Sunday December 13, 2015 @03:26AM
(#10725767)
You don't need to know the number of the line you're looking at. You jump to the middle of the file, and you compare the nearest line to the search pattern. Then you recurse on the top or bottom half.
Re:look(1): lines beginning with given string
by userbinator on Sunday December 13, 2015 @12:23PM
(#10726980)
That still requires random seeking; it's not so bad on SSDs, but abysmal on rotating disks, and most of the filesystem and OS caching code is optimised for linear forward reads, so things like prefetching are not going to help at all.
Re:look(1): lines beginning with given string
by mtdewcmu on Sunday December 13, 2015 @02:07PM
(#10727420)
At some point the cost of scanning a long file will overcome the cost of doing log(n) seeks. On short files, it really doesn't matter what you do, searching will be fast regardless.
Re:look(1): lines beginning with given string
by alayne on Sunday December 13, 2015 @02:19AM
(#10725677)
It's probably much faster in most cases of text files though.
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.