advertisement

Listen Print Discuss

Electronic Archaeology
Pages: 1, 2, 3, 4

grep

The grep program searches a set of files for a given string. It is very useful for finding out where to define and use a variable. For example:



grep regdump *.[ch]
embed.h:#define regdump                 Perl_regdump
embed.h:#define regdump(a)              Perl_regdump(aTHX_a)
proto.h:PERL_CALLCONV void      Perl_regdump(pTHX_regexp* r);
regcomp.c:# define Perl_regdump my_regdump
regcomp.c: DEBUG_r(regdump(r));
regcomp.c: - regdump - dump a regexp onto Perl_debug_log 
          in vaguely comprehensible form
regcomp.c:Perl_regdump(pTHX_regexp *r)
regexec.c:# define Perl_regdump my_regdump

This example searches all the .c and .h files for the name regdump. But sometimes we want to search all the files. The grep command can do this, but searching binary files produces a lot of junk. Binary characters can do mean things to terminals, so we need a way to convert them into something printable.

So, to search a complete set of files (including binary ones), use the command:

grep regdump * | cat -v | cut -c 1-80

cat -v
Turns unprintable characters into something readable.

cut -c 1-80
Binary files have long "lines". This command trims them to 80 characters long for viewing and printing.

Example:

grep regdump * | cat -v | cut -c 1-80
grep: Cross: Is a directory
grep: NetWare: Is a directory
...
embed.fnc:Ap    |void   |regdump        |regexp* r
embed.h:#define regdump                 Perl_regdump
embed.h:#define regdump(a)              Perl_regdump(aTHX_a)
global.sym:Perl_regdump
libperl.a:^@^PM-^K@^PMPM-^KM-^J^@^PM-^KM-^J^@
libperl.a:^^@^@^@^H^@^@^@P^@^@^@^A^@^@^@^@^@^
miniperl:^Hn^@-^N^^@^LM-3^D^H7^@^@^@^R^@^@^@s
perl:^@R^@^M^@`M-F      ^H1^@^@^@^R^@^M^@M-^WM-^
....

grep and Vim

The Vim editor can be used to view the results of a grep command. For example:

grep regdump * | cat -v | gvim -

Useful Vim commands:

:set nowrap
Don't write lines.

gf
Go to the file who's name is under the cursor.

find and grep

The find command is useful for going through a directory tree and locating files. The grep command searches files for a given text string. You can combine the two to create a system for searching a directory tree for a variable.

find . \( -name "*.cpp" -o -name "*.h" \) 
   -exec fgrep what {} /dev/null \;
find .Find starting at current directory
\( \)Group operation
-name "*.cpp" -o -name "*.h" All C++ or H files
-execCommand to execute
-exec fgrepExecute fgrep command
{}On the current file
/dev/nullAlso search /dev/null
\;End of command

Note: fgrep only prints the filename when two files are searched, thus /dev/null.

Here's an example:

find . -name *.[ch] -exec fgrep regdump {} /dev/null \;
./embed.h:#define regdump			Perl_regdump
./embed.h:#define regdump(a)Perl_regdump()
./ext/re/re_exec.c:#define Perl_regdump my_regdump
./ext/re/re_comp.c:#define Perl_regdump my_regdump
./ext/re/re_comp.c: DEBUG_r(regdump(r));
./ext/re/re_comp.c: - regdump - dump a regexp onto Perl_debug_log 
                   in vaguely comprehensible form
./ext/re/re_comp.c:Perl_regdump(pTHX_ regexp *r)
....

GNU grep and the -r (recursive) Option

The GNU version of grep has a -r option, which allows you to recursively search a directory tree. For example:

fgrep -r regdump .| cat -v | cut -c 1-80

find/grep versus grep -r
  find/grep grep -r
Speed Not that fast Faster
Can be limited to certain files (i.e. *.c)? Yes No
Standard on all UNIX systems? Yes No

Pages: 1, 2, 3, 4

Next Pagearrow