home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5264 < prev    next >
Encoding:
Text File  |  1993-01-06  |  1.8 KB  |  57 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!gatech!udel!rochester!rocksanne!news
  3. From: randy@ess.mc.xerox.com (Randy Stegbauer)
  4. Subject: Re: need a bit of help in
  5. Message-ID: <1993Jan5.140031.4710@spectrum.xerox.com>
  6. Sender: news@spectrum.xerox.com
  7. Reply-To: stegbauer.wbst129@xerox.com
  8. Organization: Xerox, Rochester, NY
  9. References: <1993Jan4.181902.20122@newscan.canada.sun.com>
  10. Date: Tue, 5 Jan 1993 14:00:31 GMT
  11. Lines: 44
  12.  
  13. In article 23031@mnemosyne.cs.du.edu, rduta@nyx.cs.du.edu (Radu) writes:
  14. >>
  15. >>2) I was wandering what is the easiest way to do a recursive grep, where
  16.  
  17. In article 20122@newscan.canada.sun.com, beric@sunvcr.canada.sun.com (Beric Maass - Sun Vancouver SPS) writes:
  18. >
  19. >find . -type f -exec grep -i string_to_search /dev/null {} \;
  20.  
  21.  
  22. This works fine when none of the directories are symbolic links.  However 'find' does not follow links.  I use the following shell script for recursive greps.  (Please excuse the fact that it is a csh script.  I wrote it before I was enlightened to sh programming.)
  23.  
  24. --------------------- BEGIN script rgrep ---------------------
  25. #!/bin/csh -f
  26.  
  27. if ($#argv < 3) then
  28.     set _program = $0
  29.     echo usage: $_program:t directory pattern_to_find files_to_search...
  30.     echo        use single quotes around 'pattern' and 'files'
  31.     echo        if they contain meta-characters.
  32.     exit 1
  33. else
  34.     set _curr_dir = $1; shift
  35.     set _pattern = "$1"; shift
  36.     cd $_curr_dir
  37.     grep "$_pattern" $argv /dev/null |& \
  38.         sed -e "/^grep: .*: No /D" \
  39.             -e "/^grep: .*: Is a directory/D" \
  40.             -e "/^No match./D"\
  41.             -e "s@^@$cwd/@"
  42.     set _next_dirs = ( .[^\.] .??* * ) >& /dev/null
  43.     if ( $#_next_dirs != 0 ) then
  44.         foreach file ( $_next_dirs )
  45.             if ( -d $file ) $0 $file "$_pattern" "$argv"
  46.         end
  47.     endif
  48. endif
  49. --------------------- END script rgrep ---------------------
  50.  
  51. ---
  52. Randy Stegbauer
  53. Phone: (716)422-3236
  54. Mail: stegbauer.wbst129@xerox.com
  55.  
  56.  
  57.