Articles to check out:

* https://www.digitalocean.com/community/tutorials/how-to-install-and-use-ack-a-grep-replacement-for-developers-on-ubuntu-14-04
* http://beyondgrep.com/why-ack/
* http://readwrite.com/2010/11/10/how-to-search-your-source-with

GREP (rinIE) or EGREP (rinI)

the all round search utility

grep -rinIE "match word" /folder
grep -rinIE "match word" /folder
egrep -rinI "match word" /folder

NOTE: the grep article recommend -inIEr, but thats hard to remember, so I prupose rinIE. which is easier because its like a wierd english name for a puppy. rinie. With the last to letters capitalized. Rather then some middle two letters capitalized.

-r makes it look recursively thru folder
-i doesnt care about case
-I skips looking thru binary files (it has a smart algo to find out which files are binary so it can skip them)
-E with regular grep makes it an egrep. If your using egrep you can skip using -E. -E allows you more regex operations for better searching.
ex: If I want to search for the words potato or hamster. I could do this: grep -e “potato” -e “hamster”  or with egrep “potato|hamster”  or with grep -E “potato|hamster”
“match folder” is the regular expression or word you want to look for
/folder is where you want to look. If you want to look thru the current directory and its subfolders & files, use “.”

Then if you want to look around use -A# -B# -C#
With -A5 for every match it will show you the results of 5 lines after
With -B5 for every match it will show you the results of 5 lines befores as well
With -C5 for every match it will show you 5 lines before and after

grep -rinIE -A5 "match word" /folder
grep -rinIE -B5 "match word" /folder
egrep -rinI -C6 "match word" /folder

Dont forget -w, which matchs only a word. So for example

grep -rinIE "or" /folder

Will match “or” “for” “floor”
But if you just want to find the word “or” add the w

grep -rinIEw "or" /folder

 

ACK

the source code search tool made with the powers of perl

An even better tool is ACK which is a program with a soul purpose of looking for source code. So it skips binary files and it has a nicer way of outputting results then grep.

apt-get install ack-grep

Now to use ack you have to type “ack-grep”, which is too long. So lets dpkg-divert it or symlink it to its proper and actual name “ack”

dpkg-divert --local --divert /usr/bin/ack --rename --add /usr/bin/ack-grep
# Or symlink (the above method just moves it and I assume does a few other things to tidy up)
ln -s /usr/bin/ack-grep /usr/bin/ack

To use ack, navigate to the root of source code heirarchy

cd /yourprogram

To list all of the files it will search

ack -f | wc -l

Grep would of looked thru all of these

find -type f | wc -l

Of course grep -I would look thru less as it will not look thru binaries. However ack has an even better sense of what to look thru.

ack "search word"

Also you can mess around with -w, -A, -B, -C just like with grep.

Leave a Reply

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