Home How To Search Your Source With Grep

How To Search Your Source With Grep

One of the joys of the last decade has been the growing ubiquity of Unix, and one of its most useful tools is grep. Even if you’re using an IDE for your development, the odds are that you’ll be able to search your code faster and more flexibly once you learn this command line tool. It’s not always obvious how to access all that power though, so I’ll show you some of my favorite techniques. The examples have been tested on OS X’s GNU grep, but should be applicable to most versions. You can download Cygwin if you want to run this and other Unix tools on Windows.

grep -inIEr –color=ALWAYS “Foo.*=” source/code/directory/

This is the way I use grep most frequently, and first here’s what those cryptic options mean. The lower-case “i” means that the search is going to be case-insensitive, and the “n” asks the tool to print the line number next to each match. The big “I” means grep uses some impressive guesswork to figure out which files are binary not text, and skips matching those. The upper-case “E” tells the tool to treat the pattern as a Posix extended regular expression, since the default of basic is very limited in what you can express. Finally the lower “r” prepares the tool to look at all the files and sub-folders within the main directory you specify. The last option switch causes the matched parts of the result lines to be highlighted in color.

The third argument is the pattern you want to match, and the only gotcha to watch out for here is that you need to enclose it in either single or double quotes to make sure none of the characters are misinterpreted as shell commands. Learning regular expressions can be daunting, but knowing that “.” matches any single character and “.*” matches zero or more of any character can get you surprisingly far. The last argument is the directory that contains your source code. Here’s an example:

The results are all the matching lines in the source code files, with the file name and line number at the start of each line. On OS X you can then open them up in your favorite editor by cutting and pasting the file name into the open command, eg

open maprender/src/maprender.mxml

Context

Sometimes just seeing a single line isn’t enough context to understand if it’s a match you’re interested in, and that’s where the -C option comes in handy. You specify a number of lines to show before and after the match, which gives you something a bit more like the webpage snippets search engines show for each result:

grep -inIEr –color=ALWAYS -C1 “Foo.*=” source/code/directory/

File types

Often you want to narrow down your search to files written in a particular language, and this is where you need the –include option. You specify another pattern (confusingly it’s a glob not a full regular expression), and only files with names that match it are searched.

grep -inIEr –color=ALWAYS –include=”*.php” “Foo.*=” source/code/directory/

Tell me more!

This is only scratching the surface of what you can do with grep, so here’s some further reading:

The GNU Grep Manual

A fantastic Grep Tutorial

Five simple recipes for Grep

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest tech headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.