Better Than Grep

search

I’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…

@BinaryMoon Have you tried http://t.co/9N6BiIbE ?

— Simon Prosser (@prossorguk) January 30, 2013

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.

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
04 Nov 2010

TimThumb Troubleshooting Secrets

I often get asked questions about TimThumb and why it doesn’t work in certain situations. I can generally tell what is wrong with the script within about 60 seconds of being sent a demo url. Below are my top tips...
13 Dec 2008

A quick way to speed up your website

Getting a faster internet is great – it gives us loads of opportunity to improve the browsing experience through more advanced javascript and designs – I still remember the dreadful websites of the late 90’s that took an hour to...
30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...
30 Nov 2009

WordPress Developer Cookbook

If I am totally honest there aren’t many computer books that I actually find useful, they’re generally for beginners or full of information you can get online for the low-cost of a Google search or two. WordPress Cookbook That’s why...