Perl Programming |
|||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
HOME HTML Tree Enhancing vi Graphical df Super Sorts CPU Hogs |
|||||||||||||||
|
Recursively Searching SubdirectoriesIn software development, especially on large projects, one has a frequent need to find out quickly where every occurrence of a variable name, function name, or other string literal exists in a body of source code. Unix, being tool rich, has a handful of ways to do this, but each one has its limitations and compromises. The default approach is to use grep, but this has its shortcomings. Getting grep to search through a directory tree is possible, but must be combined with the find command and/or xargs to make it work. Once you have grep searching through the subdirectories, you find that it doesn't know the difference between a source file and an executable or library. As a result, grep often lists out the contents of a binary file, sending terminal-scrambling control sequences to standard output. Enter Perl. It's relatively simple to have Perl search subdirectories, either by coding a routine for the purpose or simply having it run find and parse its output. Perl also has some operators that make it easy to check whether a file is displayable. I use:
The Perl script that resulted from this effort is called searchdown.pl. Not only does it recursively search subdirectories for the string or regular expression you provide, it lists the filename and line number for each matching occurrence. It's also very fast, and won't scramble your terminal window. Download searchdown.pl by clicking on this link. There's also another form of the script. This one has its own algorithm for recursively searching subdirectories. The previous version uses find. If you'd rather not have it call find, or want to see how to search through subdirectories, try the second version. Syntax
Example Output$ ./searchdown.pl . Math ./tree-diagram.html:2096:` The equals sign separators are there to make it easier to distinguish long lines that wrap, as they have in this example. If you prefer a more compact listing, just change the value of $show_separators from 1 to 0 near the top of the script. That's it for this installment. Until next time... |
||||||||||||||
|
HOME HTML Tree Enhancing vi Graphical df Super Sorts CPU Hogs |
|||||||||||||||