Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

petdance
  andy@petdance.com
http://www.perlbuzz.com/
AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)
Jabber: petdance@gmail.com

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Wednesday November 29, 2006
11:53 PM

Top 10 reasons to use ack

[ #31763 ]
Yesterday I posted about a snazzy trick with ack, but people pinged me asking what ack is and why they should use it.

ack is the replacement I wrote for grep, aimed at large trees of heterogeneous code.

Using it will change your life, but why? Here's my top 10 list:

  1. Searches recursively through directories by default, while ignoring .svn, CVS and other VCS directories.
    • Which would you rather type?
      $ grep pattern $(find . | grep -v .svn)
      $ ack pattern
  2. ack ignores most of the crap you don't want to search
    • VCS directories
    • blib, the Perl build directory
    • backup files like foo~
    • binary files
  3. Lets you specify file types to search, as in --perl or --nohtml.
    • Which would you rather type?
      $ grep pattern $(find . -name '*.pl' -or -name '*.pm' -or -name '*.pod' | grep -v .svn)
      $ ack --perl pattern

    Note that ack's --perl also checks the shebang lines of files without suffixes, which the find command will not.

  4. File-filtering capabilities usable without searching with ack -f. Want a list of all Perl files in a tree? Use ack -f --perl.
  5. Color highlighting of search results.
  6. Uses real Perl regular expressions, not a GNU subset.
  7. Allows you to specify output using Perl's special variables
    • Example: ack '(Mr|Mr?s). (Smith|Jones)' --output='$&'
  8. Many command-line switches are the same as in GNU grep:
    -w does word-only searching
    -c shows counts per file of matches
    -l gives the filename instead of matching lines
    etc.
  9. ack is pure Perl, so consistent across all platforms.
  10. Command name is 25% shorter. :-) Heck, it's 50% shorter compared to grep -r.

To install it, install the Perl module App::Ack. Your coding life will never be the same.

Visit the home page at http://petdance.com/ack

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • ack does include *~ backup files from (x)emacs. Can you please skip them too.
    Thanks.
  • This isn't a bug report so much as a question: why does that ack info page list all of the switches but ack's documentation doesn't?

  • Sounds like you want zsh... Most of the file selection stuff can be done with zsh's extended globbing [dotsrc.org]. Then it becomes available for every application, not just ack/grep.

    But the colour highlighting does look cool. :-)

    -Dom

    • Don't be so mean: zsh isn't for everyone out there. Remember the OS-impaired people. I know they (me among them) possibly can install a port of zsh, but installing ack is so much easier. And there is the portability issue Andy mentioned.

      Disclaimer: installing ack is easy if you have a working setting for CPAN, which is not that hard.

      • Well, there's always cygwin if you want bash or zsh under windows. It's not for everybody, but I find it to be a tolerable working environment when I'm in Windows.

        But I more just wanted to point out a feature that I use everyday. grep Foo **/*.pm is really handy and I think more people should use zsh and take advantage of these features!

        -Dom

  • With a little exaggeration, I appreciate ack so much that I have adopted it with the same status of Unix utilities like grep, find, wc, etc. ack is brutally useful in daily development tasks.

    The title of the comment is reminiscent to Andy's comment on corelist (which is much more Perl specific).

  • I'm interested, because using :grep in vim fills up my buffer list with blib/lib files.

    If I could get :grep --exclude to work, I might be less interested.

    What's the correct way to use grep --exclude to exclude blib/lib files? I can never get it to work.
    • The current release of GNU grep can't do it.

      The CVS tree has support for a new option --exclude-dir, which will allow you to put things like these into your $GREP_OPTIONS environment variable:

      • --exclude-dir=blib
      • --exclude-dir=.svn
      • --exclude-dir=CVS

      Though, that said, it's not clear to me how soon there'll be a release of GNU grep supporting that feature.

    • Don't bother with the grep. Get ack to take care of it. It ignores .svn and blib, and you can use --perl to only find Perl files.
      --

      --
      xoa