Live data from Hacker News

look(1): lines beginning with given string

linux.die.net

11–20 of 47 posts

Re: look(1): lines beginning with given string

#11
post #8

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.

Your .zshrc is almost certainly not a sorted file.

Re: look(1): lines beginning with given string

#12
post #8

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.

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.

Re: look(1): lines beginning with given string

#13
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.

Re: look(1): lines beginning with given string

#14

Earlier quoted context omitted.

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

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/

There's some real effort that was put into it: https://lists.freebsd.org/pipermail/freebsd-current/2010-Aug.... I don't know how it compares to all the alternatives.

Re: look(1): lines beginning with given string

#15
post #12
post #8

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.

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.

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

Re: look(1): lines beginning with given string

#16

Earlier quoted context omitted.

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

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/

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

via

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

(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_sil...

Re: look(1): lines beginning with given string

#18
post #9
post #3

Earlier quoted context omitted.

Some others I like: rev, vipe, comm, bfr, xxd

Do you know of a good resource listing all of these?

The standardized ones are described in the Shell & Utilities section of POSIX:2008/SUSv4 online at http://pubs.opengroup.org/onlinepubs/9699919799/

Re: look(1): lines beginning with given string

#19
post #15
post #12

Earlier quoted context omitted.

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.

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

[deleted]

Re: look(1): lines beginning with given string

#20
post #15
post #12

Earlier quoted context omitted.

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.

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

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

Try setting LC_COLLATE=C and export it and retry.

Post reply on HN