Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Top 10 reasons to start using ack

by petdance (Parson)
on Nov 30, 2006 at 04:46 UTC ( [id://586862]=CUFP: print w/replies, xml ) Need Help??

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

xoxo,
Andy

Replies are listed 'Best First'.
Re: Top 10 reasons to start using ack
by diotalevi (Canon) on Nov 30, 2006 at 05:21 UTC

    I wrote diotalevi's grep. It's one distinguishing feature is automatically looking inside tarballs and zip files. I find that's really common for me. It does a dirt simple look at the file extension and opens .tgz/.zip/.whatever using the streaming abilities of archive extraction programs. The code isn't sexy and it isn't even good but it does do a pretty good enough job. It'd be possible to do a much better job given a few minutes and the interest. I'd use ack immediately if it got this feature.

    sub open_file_harder { my ($filename) = @_; return if not defined $filename; if ( my ($extension) = $filename =~ /(\.[^.]+)\z/mx ) { my @readers = ( [ qr/\.t(?:ar\.)?gz\z/ => qw( gzcat ), $filename ], [ qr/\.zip\z/, => qw( unzip -p ), $filename ], [ qr/\.Z\z/ => qw( zcat ), $filename ], [ qr/\.gz\z/ => qw( gzcat ), $filename ], [ qr/\.bz2\z/ => qw( bzcat ), $filename ], ); for my $reader (@readers) { my ( $pattern, @command ) = @{$reader}; if ( $extension =~ $pattern ) { open3( undef, my $fh, undef, @command ); return $fh; } } } open my $fh, '<', $filename or die "Couldn't open $filename: $!"; return $fh; }

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      A couple of thoughts:

      * I wouldn't have it on by default. It would only be via the -A,--archive switch, for example.

      * How would I show the resultant filename? If the file is in a tarball, what do I show for the filename?

      xoxo,
      Andy

        I'd be sane to treat these containers like directories. Directories are just another kind of container. Given a tarball baz.tgz in /foo/bar containing the files a and b/c you'd tell the user about matches in the paths /foo/bar/baz.tgz/a and /foo/bar/baz.tgz/b/c. I think Windows "Compressed Folders" work like this.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Top 10 reasons to start using ack
by Ovid (Cardinal) on Nov 30, 2006 at 13:42 UTC

    I love ack, but I'd strongly recommend .ackrc support. For example, one project I work on has non-standard extensions for Perl files, so ack doesn't recognize them as being Perl. It would be nice to have an .ackrc so that I can provide those extra extensions to add on to ack file recognition or to specify default switches. Of course, this would require an extra switch to ack to ensure that the .ackrc is not read, if necessary.

    It would be a nice feature for prove, also. The vast majority of time I just type prove -l t. For prove, you could ignore the .proverc if you have any arguments. For ack, you could ignore the .ackrc if you have more than one argument.

    Cheers,
    Ovid

    New address of my CGI Course.

      ack is really nice; just this morning I've started using it in favor of my bash wrappers around grep. I agree with Ovid, though -- .ackrc support would be super-keen. ACK_SWITCHES is good, but say I want a different set of options per project?

      Minor quibble, though. Nice work, petdance.


      --
      man with no legs, inc.
      Really what you're asking for is not .ackrc support, but support for specifying filetypes.

      xoxo,
      Andy

Re: Top 10 reasons to start using ack
by duelafn (Parson) on Nov 30, 2006 at 16:01 UTC

    I'm not sure that completely ignoring unrecognised text files is the best form of DWIM. It took me several searches to realize that ack does not recognise TeX files. I suspect that this would mean that extensionless plain text files would not be searched since they have no #! line. Having to specify the --all option to search these files takes away some of the fun since then backup (~) files are included then. Perhaps a --text option (anything matching -T that is not a backup file) as a counterpart to the --binary option would be nice.

    Good Day,
        Dean

Re: Top 10 reasons to start using ack
by jmcnamara (Monsignor) on Nov 30, 2006 at 09:07 UTC

    I've been using ack for a while on Linux and Solaris and it is genuinely very useful. Well done.

    --
    John.

Re: Top 10 reasons to start using ack
by parv (Parson) on Dec 01, 2006 at 05:28 UTC

    You gave me a few ideas that I should include in my own file searching program, namely ...

    • by default ignore CVS & .svn directories (i am tired of filtering find(1) output) (1);
    • specify types of files to search (3).

    Thanks much.

    BTW, I got 403 error when I tried to access http://petdance.com/ack.

      I got 403 error when I tried to access http://petdance.com/ack.

      Ack is currently/permanently hosted at beyondgrep.com.

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
Re: Top 10 reasons to start using ack
by Cody Pendant (Prior) on Dec 06, 2006 at 05:49 UTC
    OK, I have to ask ... why is it called "ack"?


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
      Because it's short and pronouncable.

      xoxo,
      Andy

        Darn. I was hoping for something more creative involving Bill the Cat and Bloom County, even if it's made up...
        Update: You know, I totally forgot about --thppttt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://586862]
Approved by grep
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2024-03-28 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found