Combining ack-grep and xargs

Gotta love the command line..

I use ack-grep a lot and really, really like it. Kudos to the author and to the maintainer who takes care of the ubuntu package πŸ™‚

Sometimes though I was missing grep‘s --exclude feature that allows me to ignore certain paths while searching.

There are occasions where I e.g. want to see calls to a certain function in the code base but I am not interested in tests. Today I found an (embarrassingly) easy way to get that behaviour using xargs:

$ find . -name \*.py | grep -v tests/ | xargs ack-grep -C 3 -w 'Message\('

The snippet above first accumulates the paths of interests, then filters them and finally lets ack-grep loose on them.

Ta-da! There you go πŸ™‚


7 thoughts on “Combining ack-grep and xargs

  1. Let ack do the searching for the python for you.

    ack –python –ignore-dir=tests/ -C 3 ‘Message\(‘

    I added the –python (and other languages) specifically to avoid the “find . -name” stuff.

  2. Oh wow! I totally missed the ‘–ignore-dir’ command line arg πŸ˜›
    That will do the job in the vast majority of the cases, I agree.

    Thanks for the pointer!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s