Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Recursive Grep

by sierrathedog04 (Hermit)
on Jun 19, 2001 at 02:17 UTC ( [id://89501]=CUFP: print w/replies, xml ) Need Help??

Recursive Search and Grep Utility Written by sierrathedog04, sierrathedog04@hotmail.com, 6/01

  • The first parameter is a regular expression which matches the filenames that you wish to search for. It defaults to '.*'
  • The second parameter is a regular expression for which you wish to grep your found files. It defaults to '.*'
  • The third parameter is the directory from which you will start your recursive search. It defaults to '.'
Tested on Perl 5.005_02 in a MP-RAS Unix environment.
Updated 6/19 to add line inadvertently left off original posting.
use strict; use File::Find; my $glob = shift || '.*'; my $grepTerm = shift || '.*'; my $dir = shift || "."; #following line added 6/19. (Inadvertently left off original posting:) find (\&Wanted, $dir); sub Wanted { return unless /$glob/; open (CURRENT_FILE, $_); my @currentFile = <CURRENT_FILE>; my @foundLines = grep /$grepTerm/, @currentFile; print "In $File::Find::dir/$_:\n" if @foundLines; foreach (@foundLines) { print " $_\n"; } } print "Completed recursive search for files named $glob containing ter +m $grepTerm in directory $dir";

Replies are listed 'Best First'.
Knob Re: Recursive Grep
by knobunc (Pilgrim) on Jun 19, 2001 at 16:14 UTC

    That works nicely. Alternately:

    find . -type f | grep -v CVS | xargs egrep

    If you alias that to something (e.g. superfind) you can do:

    superfind -i whatever

    Of course the intermediate grep can be eliminated if you don't have standard things to ignore. Also you can replace the egrep with your favorite grep-like substitute. By having the egrep bare at the end you can pass whatever flags you want to it.

    The advantage of the above is that it is probably faster, and I can remember & type it quickly when moving from machine to machine. The downside is that I don't have all of Perl's regex language (but egrep usually has all I need for this), it will not work on non-Unix boxes without additional packages, and that I can't customize it more as needed to control the exact matches. However, xargs is a cool and often overlooked tool.

    -ben

      There is a grep written in Perl, tcgrep, that supports all of Perl's regex by nature.
Re: Recursive Grep
by John M. Dlugosz (Monsignor) on Jun 19, 2001 at 19:42 UTC
    A couple comments:

    The Wanted function is never called. If that's supposed to be a complete script, is there some lines missing?

    Use the o modifier on your re's for better performance.

      Good catch. The following line was left off just before the subroutine is declared:
      find (\&Wanted, $dir);
Re: Recursive Grep
by Anonymous Monk on Jul 01, 2010 at 19:52 UTC
    I believe you are missing a call to close on the filehandle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://89501]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found