Unix Recursive Search
CAT is a UNIX command that displays the contents of files. It is part of the core linux utilities and comes as part of most linux distributions.
A powerful method of using CAT is piped with other commands. For example, if you want to search for something inside a text file, you could use:
cat HelloWorld.java | egrep ';$'
The previous command will pass the contents of HelloWorld.java to the next command EGREP. EGREP displays lines that contain the regular expression ‘;$’. That particular regular expression displays lines that end with a semi-colon.
This command can be used to search a source file for particular text. Rarely, however, do you want to know if a single file contians text. As a programmer, I would find it much more useful to search files from a particular directory for text. The following command will list lines from all java files in the directory:
cat *.java | egrep '^public class'
The problem with the above command is it will not search files recursively. That regular expression lists lines that begin with ‘public class’. The command CAT does not have a -r option (for example), to recursively display files.
BASH scripting can be messy, however here is a single line statement that will list all files recursively.
for a in `find `; do echo [$a]; cat $a | egrep ”; done
I will dissect it one command at a time:
The ‘for’ command is used to process each line that is resulted from the file command (see below). That way we can process each file individually. The letter ‘a’ is used as the variable that each line is put into.
The find command is similar to the ls command, except it lists files with their full paths. We need the paths to use to read each file. The single quotes (note it is the one on the ~ key) are used to tell the interpreter to execute the command inside then use the output in that position.
All commands between the ‘do’ and ‘done’ are executed for each line executed.
The text that is executed for each line is:
echo [$a]; cat $a | egrep ”;
Looks very similar to the very first command at the top of this page. It prints the contents of the file in the $a variable to standard output, and then EGREP filters each line by the expression provided. The echo command displays the filename so we know which file the search result is related to.
Here is an example:
for a in `find *.java`; do echo [$a]; cat $a | egrep ‘^public class’; done
That will display all java files in this and all subdirectories, and return lines that begin with ‘public class’.
Update:
Using ZSH, and unix tools for windows, the following will do the same:
for a in `c:/bin/find.exe | egrep '.cs$'`; do echo; echo \[$a\]:; cat $a | nl | egrep ‘ClientListDAO’; done