home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13456 < prev    next >
Encoding:
Text File  |  1992-11-15  |  2.4 KB  |  82 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: how to search/replace over an entire directory
  5. Message-ID: <1992Nov16.022048.27425@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: Sequoia Peripherals, Inc.
  8. References: <SCOTTH.92Nov14103759@kuwait.gdfwc3>
  9. Distribution: comp.unix.questions
  10. Date: Mon, 16 Nov 92 02:20:48 GMT
  11. Lines: 69
  12.  
  13. Email bounced. Pardon me for posting something that is not really
  14. clean enough for general consumption.
  15.  
  16. gdfwc3!kuwait!scotth@uunet.uu.net writes:
  17. >Is there an easy way to do a search and replace of a string over an 
  18. >entire directory or a list of files.  It would be nice it was something
  19. >like like
  20. >
  21. >$ search_replace -search string1 -replace string2 -directory this_directory
  22.  
  23. This should work. I changed the usage to 
  24.  
  25.    search_replace string1 string2 this_directory
  26.  
  27. because as far as I could see, the -search, -replace, -directory
  28. weren't buying you anything. It would be easy enough to adjust.
  29.  
  30. And I also am assuming that none of the arguments has any
  31. spaces in it or any characters (such as .) that have special
  32. meaning as regular expressions. All of this could be dealt
  33. with by quoting things appropriately, but this is just a
  34. quick off-the-top-of-my-head solution.
  35.  
  36. I also assume your system supports the #! syntax for launching
  37. scripts. If not, figure out some other way to get /bin/sh
  38. to interpret this.
  39.  
  40. Put this into a file and execute it as
  41.  
  42.    filename string1 string2 this_directory
  43.  
  44. #! /bin/sh
  45.  
  46. [ $# -eq 3 ] || {
  47.      echo "Usage: $0 srchstring replstring dirname" >&2
  48.      exit 1
  49. }
  50.  
  51. srch=$1
  52. repl=$2
  53. dname=$3
  54.  
  55. find $dname -type f -print |
  56.      while read fname
  57.      do
  58.        ed - $fname <<-EOF
  59.        1,\$s/$srch/$repl/g
  60.        w
  61.        q
  62.      done
  63.         
  64. exit 0
  65.  
  66.  
  67. I tested it out and it worked, but I do tend to make mistakes when
  68. using ed, especially when I don't have my docs at hand. I'd test it
  69. before unleashing it on real data.
  70.  
  71. If you have any questions about how either of this script works or 
  72. about how to improve it to add better error checking or more flexibility, 
  73. feel free to drop me a note. Shell programming can be tricky.
  74.  
  75.  
  76.  
  77. -- 
  78. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  79. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  80.        There's something in my library to offend everybody. 
  81.           --- Washington Coalition Against Censorship
  82.