look ac | grep date
look(1): lines beginning with given string
41–47 of 47 posts
Re: look(1): lines beginning with given string
#42If you don't have this basic linux utility, you can use grep '^string' file.txt but there's a greater chance of having look installed than grep.
Re: look(1): lines beginning with given string
#43Earlier quoted context omitted.
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.
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
#44Re: look(1): lines beginning with given string
#45The universe of Unix filters never ceases to turn up things I haven't seen before. Other examples for those inclined: tac, sponge, pv, join, paste.
In the same vein of 'improvements over things everyone assumes you have to do with the more famous tool', we could say grep '^string' : look :: sort -R : shuf
Re: look(1): lines beginning with given string
#46Earlier quoted context omitted.
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.
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
#47Earlier quoted context omitted.
In the same vein of 'improvements over things everyone assumes you have to do with the more famous tool', we could say grep '^string' : look :: sort -R : shuf
Thanks to @climagic on Twitter, I recently learned that sort -R and shuf are not in fact the same thing. (sort -R doesn't actually shuffle...)