How To Search Your Source With GrepFirst lession in Unix 101
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
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/
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:Discuss