Live data from Hacker News

Use ack instead of grep to parse text files

stevengharms.com

41–50 of 51 posts

Re: Use ack instead of grep to parse text files

#41
post #39

Is anyone else turned off by the tone of the blog post and the attitude of the poster? There's gotta be a better way to showcase the usefulness of a tool.

I'm more turned off by hard-to-read regular expressions, especially ones that look like they may break depending on what terminal emulator I'm using, how quotes are escaped, etc. The "you've been doing it wrong for years" tone I could do without, but see people using it with good enough intentions so frequently that I'm no longer bothered by it.

Also, `ack` is not installed by default, which is reason enough to not get too used to it. Some people will say "optimize for being on your own machine, since you are 99% of the time", but I'm not. Installing additional utilities on multiple production servers is annoying enough, and can actually become problematic in a PCI-compliant environment as mine is. I'm also frequently helping out other members of my team, and having a magic one-liner that often results in "-bash: ack: command not found" is not terribly useful to me. YMMV.

Re: Use ack instead of grep to parse text files

#42
post #18

You can do a lot of things with `grep -E`, fwiw - there's not much here to really sell ack. Things that do sell ack, for me: ack css_class --sass # search .sass and .scss ack some_method --no-flash # ignore .as and .mxml # ignore compiled css in every Rails project on # my system (as long as I `ack` from the root) --ignore-dir=public/stylesheets/compiled And the fact that it prints out like this: path/to/file.ext 123…

Entirely agreed. Other users' points about the tone of the article ring true to me as well, and they are only hurting people's impression of the tool, which is unfortunate; I use both ack and grep (my regex comfort level is not extremely high, so grep -v is still a common fallback).

For the curious: It's "ack-grep" in Ubuntu's package manager (and presumably Debian, though I can't say for sure); I stick it on every machine/server I set up just to have it handy. Queries to the effect of ack-grep --python ClassName yield fast, readable, extremely useful output, as you mention. That's why I use it in addition to grep.

Re: Use ack instead of grep to parse text files

#43
- grep does regular expressions.

- grep uses by default the same regular expressions as sed, which is another frequently used tool.

- grep also supports perl regular expressions.

- grep is available on every linux/bsd/*nix system out there, so it just works and make your scripts work.

- We use grep to search through gigabyte sized files (ie logs). You didn't show us how well ack performs there.

Re: Use ack instead of grep to parse text files

#44

Earlier quoted context omitted.

Are you confusing awk with ack?

I confused the apps in the text but the point was that those regexes look far worse than the equivalent grep pairs.

Agreed. He says he can't remember the syntax for such and such in grep, but the regexen he follows up with seem complicated enough to me.

Now, there's stuff that never sticks in my brain (tests in shell, sigh). But generally there's less syntax and therefore less to remember in a chain of greps. Composition of simple piece is easier to understand than one equivalent and therefore more complex piece. Heck, the power of the shell is predicated on this idea.

Perhaps the best part about ack is that it's simple to restrict your search of files to a given pattern with a command line flag rather than using shell globbing. You could wrap invocations of grep with a shell function or another script, but that's still not great.

Re: Use ack instead of grep to parse text files

#45

- grep does regular expressions. - grep uses by default the same regular expressions as sed, which is another frequently used tool. - grep also supports perl regular expressions. - grep is available on every linux/bsd/*nix system out there, so it just works and make your scripts work. - We use grep to search through gigabyte sized files (ie logs). You didn't show us how well ack performs there.

That'd be my other concern. ack is perl, as far as I can tell. I have no idea how perl performs at these tasks. But grep is written in C, and there're fun examples of how exactly it gets to be so fast (http://ridiculousfish.com/blog/posts/old-age-and-treachery.h... comes to mind).

Re: Use ack instead of grep to parse text files

#47
post #16

Is it worth learning grep or ack or a similar tool? When I need to do these sort of tasks, I do them in a scripting language with some combination of split() and regex instead of using command line tools. But, I'm just doing that because it's what I know. Would I end up saving a significant amount of time if I learned to use grep instead?

In my opinion, yes, it is. grep is fast and versatile.

Shell tools in general are relatively simple or at least specialized, and they're built to be composed in novel/useful ways. The interface between all of these is text, aka data, aka what is arguably the simplest interface.

Re: Use ack instead of grep to parse text files

#48
ack-grep is not ack's related cousin. Ack-grep is the name debian uses for the ack executable in order avoid a namespace collision.

  dfc@ronin:~$ apt-file search bin/ack
  ack: /usr/bin/ack
  ack-grep: /usr/bin/ack-grep
  ...
  dfc@ronin:~$ apt-cache search --names-only ^ack
  ack - Kanji code converter
  ack-grep - grep-like program specifically for large source trees
  dfc@ronin:~$

Re: Use ack instead of grep to parse text files

#50

  cp /usr/bin/grep ack
Find the needles

   ./ack needle haystack
Find the silver needles

   ./ack silver.*needle
Find all needles except lead ones

   ./ack '[^^][^e.][^a.][^d.] needle' haystack
That last one could be tricky if there's other types of needles with names like "ead needle" or "mead needle". But using the haystack he gives us BRE can do the job, easily.

Perl regex may be easy to use but they are inferior from a performance perspective. As someone else said, they're slower than BRE or ERE. Moreover, even if speed is not an issue, you pay a price in the amount of memory you will need compared with line-based utilities like, e.g., sed and awk.

Find the needles

  sed '/needle/!d;/needle/q' haystack
Find the silver needles

  sed '/silver needle/!d;/silver needle/q' haystack
Find all needles except lead ones

  sed '/lead needle/d;/needle/!d/needle/q' haystack
My preference is to use (f)lex if I want a fast "parser" (scanner). Its regex is more than adequate.
Post reply on HN