
look(1): lines beginning with given string - mtdewcmu
http://linux.die.net/man/1/look
======
jzwinck
The 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.

~~~
cfcef
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

~~~
nhaehnle
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...)

~~~
cfcef
My point there was that besides being a true shuffle, 'shuf' also has an
algorithmic advantage when you only need a few lines (similar to how lookup
has an advantage when you only need a few).

------
gizmo686
On my machine, (Ubuntu 14.04) look only performs a binary search if the "-b"
option is passed (unless no file is specified, in which case it behaves as
described in the linked manpage). My manpage notes in the compatibility
section:

 _look uses a linear search by default instead of a binary search, which is
what most other implementations use by default._

------
nonotmeplease
Is look of any use? Isn't grep fast enough?

~~~
LukeShu
A better one-line description would be

"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.

~~~
nonotmeplease
I created a 1.1G file with dictionary words and timed...

    
    
      % 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).

~~~
justinator
Is grep optimized to be fast, though? There seems to be a lot of, "better at
grep than grep" tools out there,

[http://beyondgrep.com/](http://beyondgrep.com/)

~~~
hyperpape
There's some real effort that was put into it:
[https://lists.freebsd.org/pipermail/freebsd-
current/2010-Aug...](https://lists.freebsd.org/pipermail/freebsd-
current/2010-August/019310.html). I don't know how it compares to all the
alternatives.

------
hanez
When running 'look' to get all comments in a file I only get the first found
line in the ouput. Can someone explain this?

look "#" .zshrc

There are really a lot more comments (lines starting with '#') in my .zshrc
then the first line.

~~~
kbd
From the manpage:

> As look performs a binary search, the lines in file must be sorted...

I assume you didn't sort your source file first.

~~~
hanez
When sorting the file first I get no output anymore:

sort ~/.zshrc -o /tmp/.zshrc

look "#" /tmp/.zshrc

Sorry for the noise but I am confused... :|

~~~
emmelaich
sort(1) might not use the same ordering as look(1)

Try setting LC_COLLATE=C and export it and retry.

~~~
hanez
Uh, wow! That was it!

Thanks for this answer...

------
grymoire1
One of the useful things about look is that it defaults to the dictionary. So
if you weren't sure how to spell accommodate, you could type

look ac | grep date

------
hyperpape
Only 12 hours after looking at this did I realize that if your log files have
one record per line, you can easily use this to search for all logs that
happened in a particular ten minute period.

------
selfishAIgen
look a |wc -l => 5985

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.

------
vortico
If 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.

~~~
mtdewcmu
The interesting thing about look is that it uses binary search, so it is much
faster.

~~~
discardorama
Binary search is useless unless the file is sorted. Most often my files are
not sorted, so egrep '^line' works just fine.

~~~
alayne
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?

