Pages

Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Friday, July 29, 2011

Ack!

I'm sure you've used grep at some point in your life as a programmer to search for that piece of code in your code base. I was using it myself — until today. It turns out there's a better alternative to all that time spent trying to get the perfect grep alias so it works as it should: searching recursively, ignoring the .git/.svn directory, ignoring backup files (foo~/#foo#), printing out the file names and line numbers, grouping matches by file name. You've got to listen when Jacob Kaplan-Moss, creator of Django, has this to say about it:
"Whoa, this is *so* much better than grep it's not even funny."
It's called ack. Without further ado, you can install it on your Ubuntu system using sudo apt-get install ack-grep (for other distros, check out the ack homepage). Then use it as: ack-grep search_term. It's really that simple (although the default colours are a little irritating). You can see what files/directories it ignores by default using ack-grep --help. Here's a screenshot for the impatient reader:


'nuff said. I'm gonna go explore the man page.

Sunday, June 5, 2011

Software projects and 'grep'

It might sound as weird title, especially when I emphasize that by 'software project' I refer to absolutely 'any' project. After a few days of trying to understand the source code of a few projects (including Kate), I have come to realize that it is hard to understand the entire code at once.

Realizing the impossibility of understanding the entire code-base (which could be well over a hundred thousand lines) I started trying other means. The first step was to identify the 'main' file. As I am working with C/C++, this file is easy to identify as it is the one containing the function :-
int main (int argc, char *argv[] ) 
Knowing what I am looking for, this simple command (or a few variations) allow me to spot the file where it all begins :-
grep "int main" * -R 
As should be clear, this would search the source code directory 'recursively' to find me the file with the keywords. At this point, I would like to highlight why it would be easy to maintain a program that called the 'first' function by the name 'main'. Whichever language it is written in, use of this convention can make it easier to maintain. 

Once the main file has been spotted, I proceed to find the functions in almost a similar fashion. This saves me the effort of going through pieces of code that are rarely (or at times never) used. I can follow the 'flow of program' more easily and I know which function lies where. As and when I proceed to understand the functions, I try to make certain notes about the functions, in the form of comments. These notes could be something describing the task of the function, or could point to some other piece of information, or at times hints for improving the function later.

'grep' has proved very useful to me, and although I am not very good with RegEx (regular expressions), I now plan to get hold of them so as to be able to use them to spot what I want in combination with 'egrep'.