home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3634 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  1.7 KB

  1. Path: sparky!uunet!olivea!decwrl!concert!sas!mozart.unx.sas.com!sasegb
  2. From: sasegb@toklas.unx.sas.com (Elizabeth Brownrigg)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: Sed with shell variables
  5. Message-ID: <BtLHtK.6IB@unx.sas.com>
  6. Date: 26 Aug 92 14:34:31 GMT
  7. References: <2701@nlsun1.oracle.nl>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 52
  11. Originator: sasegb@toklas.unx.sas.com
  12. Nntp-Posting-Host: toklas.unx.sas.com
  13.  
  14.  
  15. In article <2701@nlsun1.oracle.nl>, rgasch@nl.oracle.com (Robert Gasch) writes:
  16. |> Using sed to edit strings is usually a nice and practical
  17. |> method of doing so. I you want to change a pattern you usually
  18. |> say:
  19. |>      echo $something|sed 's/pattern1/pattern2/'
  20. |> 
  21. |> The problem I ran into is that I can't get shell variables to
  22. |> work as arguments. When I type
  23. |>      echo $something|sed 's/$this_pattern_var/$that_pattern_var/'
  24. |> 
  25. |> sed throws up on me. I've tried various combinations of quotation
  26. |> marks without success. Is there an easy way of doing this??
  27. |> 
  28. |> Thanks a lot
  29. |> ---> Rob
  30.  
  31.  
  32.  Here's a script I wrote using sed and shell variables to globally
  33. search and replace strings in all the files in a subdirectory:
  34.  
  35.  
  36. echo "Enter the search string.  Precede regular expressions with
  37. a double backslash."
  38.  
  39. read firststring
  40.  
  41. echo "Enter the replacement string.  Precede regular expressions
  42. with a double backslash."
  43.  
  44. read secondstring
  45.  
  46. for file in *
  47.  
  48. do
  49.     echo 'Starting file '$file
  50.  
  51. # Use the sed editor to search and replace firststring with
  52. # secondstring in each file in the subdirectory.  Write the corrected 
  53. # file to file.sed.
  54.     sed "s/$firststring/$secondstring/g" $file >$file.sed
  55.  
  56.     rm $file
  57.     mv $file.sed $file
  58.  
  59.     echo 'Ending file '$file
  60.  
  61. done
  62.  
  63.  
  64.  
  65. - Elizabeth
  66.