Grep To Find Code

Find code by using grep

The command line utility grep can be used to find specific code in files. I needed a way to find code snippets recently while designing a magento shopping cart. The documentation for magento is sparse.

Grep enabled me to find specific code and output the exact file name and its location. With grep, I was also able to find the exact line number for the code.

In this tutorial, a shell will be used in order to use the grep command. Shells or terminals are available for Unix, Linux and Macs. There are grep tools for windows ranging from virtual terminals like Putty to Linux-like environments like Cygwin.

    Tools are required:

  • Grep.
  • Optional Text editor for creating and modifying files.
  • Shell.

Optional Download and install suitable text editor

Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.

Grep: Simple

Code File Grep Command
echo “some, text, file” > test.txt # First line

echo “some, txt, infile” >> test.txt # Second line
grep “text” test.txt #Print lines matching “text”

The first echo command created a file called test.txt. The second echo command appended a new line to test.txt. The grep command searched for the word “text” and printed the matching line.

Grep: Specific

Code File Grep Command
Search Directories
mkdir testfolder1 # create testfolder1 folder

mkdir testfolder2 # create testfolder2 folder

echo “some, text, file” > testfolder1/test.txt # First line

echo “some, txt, infile” >> testfolder1/test.txt # Second line

echo “some, text, file” > testfolder2/test.txt # First line

echo “some, txt, infile” >> testfolder2/test.txt # Second line
grep “text” ./ -r #Print lines matching “text”

The mkdir command created two directories called testfolder1 and testfolder2. The echo command created two files in the two directories. The grep command searched for the word “text” recursively and printed the matching lines.

Grep: Line Number

Code File Grep Command
echo “some, text, file” > test2.txt # First line

echo “some, txt, infile” >> test2.txt # Second line
grep “text” -n test2.txt #Print line numbers matching “text”

The first echo command created a file called test2.txt. The second echo command appended a new line to test2.txt. The grep command searched for the word “text” and printed the line number and the matching line.

Conclusion:

The command line utility grep can be used to search for a matching string. The search can be recursive through all files and sub-directories. Matching lines can be printed along with the line numbers.

    Recommendations:

  1. This tutorial uses a shell or terminal.
  2. The concepts taught can be applied to find code for programming languages.
  3. For faster searches, use the most local folder.

Tags: , , , , , , ,

This entry was posted on Wednesday, October 12th, 2011 at 12:00 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>