6 ms·
look(1): lines beginning with given string
- jzwinck 11y agoThe 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.
- ryan-c 11y agoSome others I like: rev, vipe, comm, bfr, xxd
- lappa 11y agoDo you know of a good resource listing all of these?
- dap 11y ago`ls /usr/bin/?? /usr/bin/???` can be interesting to skim through.
- kevinoid 11y agoThe standardized ones are described in the Shell & Utilities section of POSIX:2008/SUSv4 online at http://pubs.opengroup.org/onlinepubs/9699919799/ http://pubs.opengroup.org/onlinepubs/9699919799/
- jph 11y agoYes, Num command has a quick list: http://www.numcommand.com/doc/helpers.html http://www.numcommand.com/doc/helpers.html
- cfcef 11y agoIn 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 11y agoThanks 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 11y agoMy 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).
- ryan-c 11y agolook does a binary search and only works on sorted files, while grep '^string' works on any text file but would be slower.
- nonotmeplease 11y agoIs look of any use? Isn't grep fast enough?
- LukeShu 11y agoA 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 11y agoI 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 11y agoIs 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 11y agoThere's some real effort that was put into it: https://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html https://lists.freebsd.org/pipermail/freebsd-current/2010-Aug.... I don't know how it compares to all the alternatives.
- DougMerritt 11y agoThe "ack" you reference is apparently faster only in that it's easier to specify exactly which files to search or not to search, which obviously can also be done with grep, e.g. combined with 'find'. 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.html http://ridiculousfish.com/blog/posts/old-age-and-treachery.h... via https://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html 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 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_silver_searcher_is_a_35x_faster_drop_in/ https://www.reddit.com/r/programming/comments/16bvah/the_sil...
- hanez 11y agoWhen 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.
- dmd 11y agoYour .zshrc is almost certainly not a sorted file.
- kbd 11y agoFrom 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 11y agoWhen 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... :|
- deleted 11y ago[deleted]
- emmelaich 11y agosort(1) might not use the same ordering as look(1) Try setting LC_COLLATE=C and export it and retry.
- hanez 11y agoUh, wow! That was it! Thanks for this answer...
- andreasvc 11y agoIt doesn't make sense to sort a configuration file, order and context matters. Easier to just use grep '^#' (or even better, ag).
- gizmo686 11y agoOn 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.
- vortico 11y agoIf 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.
- DiabloD3 11y agoAs 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.
- userbinator 11y agogrep is also specified by POSIX, whereas look isn't.
- mtdewcmu 11y agoThe interesting thing about look is that it uses binary search, so it is much faster.
- discardorama 11y agoBinary search is useless unless the file is sorted. Most often my files are not sorted, so egrep '^line' works just fine.
- mtdewcmu 11y agoThe 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.
- alayne 11y agoIt 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?
- 11y ago
- selfishAIgen 11y agolook 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.
- grymoire1 11y agoOne 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 11y agoOnly 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.