Thursday, December 2, 2010

Day 2: a better grep - ack

If you are a Unix/Linux user you propably know grep. It lets you search in the contents of files.

Back when I was using SVN as version control system, there was one annoying thing: SVN stored all versioned files twice. There always was a copy in the .svn directory. So, when I was using
grep -r 'use strict'
I also got these copies as results. By typing
grep -r 'use strict' | grep -v .svn
I only got the desired results.

It was at this time that I came across ack - and was immediately sold. Ack ignores also other version control directories and common backup files (e. g. ending with '~').

Also, it lets you specify the file types:
ack --perl 'use strict'

There are more options, have a look at the documentation.

Ack is available through CPAN - just install App::Ack. But there is also an standalone version which includes every module into one big script:
curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3

This standalone version is also used in the ack-in-project and AckMate TextMate bundle. (TextMate is a popular text editor for MacOS.) I use TextMate at work - the AckMate bundle works very well.

Ack runs also on Windows (its pure perl). Under Debian and Ubuntu the package is called ack-grep. (Ack is a Kanji code converter there.)

Links:

2 comments:

  1. Emacs users might be interested in full-ack.el. Note that if you have the latest version ack, you'll need the latest version of full-ack.el.

    ReplyDelete
  2. find & xargs are better:

    % find . -not -regex '.*\.svn.*' |xargs -P grep -H 'what you need to grep'

    ReplyDelete

Note: Only a member of this blog may post a comment.