Better Than Grep

searchI'm not that knowledgable about the command line and controlling a server with it. Over the years I have picked up a few bits - I use nano a lot for editing files, and I like to use grep to search my code.

Or at least I used to.

Ack

The other day I was trying to work out how something worked in WordPress, and I was using grep to search the WordPress code base - except it wasn't traversing the directory tree properly. So I asked on Twitter if I was doing something wrong. I got this as a reply...

So I checked out BetterThanGrep.com a quick read and it looked like what I need. Ack is a replacement for grep designed specifically for programmers.

Installing it was easy - just a single line of code to grab a Perl script from an external website (obviously caution is recommended when doing this sort of thing).

curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3

How To Use Ack

Using ack is super simple. All you have to do is navigate to the directory you want to search and then type ack 'search_term'.

To go with this there are a bunch of additional parameters that allow you to display and filter the results in the way that works best for you. For me I have only used a few of the parameters so far - I've listed them below. You can read the rest of the commands in the documentation.

Display lines either side of result

ack -A 5 -B 5 'search_query'

The A and B options display the matched line of code, and the lines before and after - with the number of them limited by the size added after the option. In the example above I limited it to 5 lines either side of the result.

Display files only and not results

ack -l 'search_query'

The l option changes the output so that it only displays the filenames of the files that were found to have the search query. I used this to narrow down my search so that I could work out what directory my problem files were in.

Binoculars Image from Shutterstock.

Share This...

About The Author

Ben Gillbanks

Web Designer, Video Gamer, Blogger, and part time Entrepreneur. Read More

4 thoughts on “Better Than Grep

  • Reply ›
    Joseph Scott

    I've found ack to be better than grep for many of the searches I use, but having grep installed every where means I still end up using it a fair bit.

    In regards to your examples, both can be done with grep as well:

    grep -A 5 -B 5 'search_query'

    If you just want 5 lines on both sides you can do that with one option, -C:

    grep -C 5 'search_query'

    The -C works for ack as well.

    And the file list match option is the same too:

    grep -l 'search_query'

    February 10, 2013

    • Reply ›
      Ben

      interesting - thanks for the pointers! I guess that shows how much I have looked into grep and its options :)

      To be honest I have been quite happy with grep until it wouldn't search recursively for me, and I couldn't work out why. Ack solved the problem and I guess that's why I spent a bit of time reading the Ack docs.

      February 10, 2013

      • Joseph Scott

        My most common use of grep is grep -ri search * the -r is for recursive, the -i is for case insensitive search. And the * of course means everything :-)

        February 11, 2013

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Websites linking here

  1. Favicon for http://www.google.com/s2/favicons?domain=bengillbanks.co.uk Better Than Grep

My Projects

TimThumb - Image Resize Script TimThumb

Image Resize Thumbnail Script

WPVote - WordPress Social Voting WPVote.com

WordPress Social Voting Site

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.

My Tweets › Binary Moon

Random Link-outs

Keep Updated

Subscribe to RSS

Stay Updated

Binary Moon

WordPress and Web Development › home of Ben Gillbanks