This should also be possible with a combination of xargs and grep, but I liked this solution better, with line numbers and path location included. Also allows users to specify a filename pattern, example searching pl or php or html files.
Comments would be very appreciated.
#!/usr/bin/perl -w # By Greg Flanders gflan@NO.avalon.SPAM.net.THANKS # # Opens each file from currenct directory and searches # for the pattern given. Jumps down directories to search # recursively. use strict; use File::Find; use Cwd; my $directory = getcwd(); my$searchstring = $ARGV[0]; my $ext = $ARGV[1]; print "$searchstring\n"; die "Usage : ggrep searchstring extension\n" if ($searchstring eq ""); find (\&process_file, "$directory"); sub process_file { return if -d; if ($_ =~ /$ext/i) { open (INPUT, "< $_"); foreach (<INPUT>) { if(/$searchstring/i) { $_ = substr($_, 0, 60); s/^\s*//; print "$File::Find::name\:$.\:\t$_\n"; } } close INPUT; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Recursively grep through directory trees.
by davorg (Chancellor) on Jul 12, 2000 at 14:05 UTC | |
by Melvin (Beadle) on Jul 12, 2000 at 17:33 UTC | |
|
RE: Recursively grep through directory trees.
by merlyn (Sage) on Jul 12, 2000 at 19:16 UTC | |
|
RE: Recursively grep through directory trees.
by ZZamboni (Curate) on Jul 12, 2000 at 23:20 UTC | |
|
(jjhorner)Recursively grep through directory trees.
by jjhorner (Hermit) on Jul 12, 2000 at 17:42 UTC | |
by Melvin (Beadle) on Jul 12, 2000 at 18:06 UTC |