home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3557 < prev    next >
Encoding:
Text File  |  1992-08-21  |  2.4 KB  |  67 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!usc!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Sed with shell variables
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Aug21.134656.20370@news.eng.convex.com>
  8. Date: Fri, 21 Aug 1992 13:46:56 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <2701@nlsun1.oracle.nl> <1992Aug21.110719.11512@siesoft.co.uk>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 49
  17.  
  18. From the keyboard of stuart@siesoft.co.uk:
  19. :rgasch@nl.oracle.com (Robert Gasch) writes:
  20. :: Using sed to edit strings is usually a nice and practical
  21. :: method of doing so. I you want to change a pattern you usually
  22. :: say:
  23. ::     echo $something|sed 's/pattern1/pattern2/'
  24. :: 
  25. :: The problem I ran into is that I can't get shell variables to
  26. :: work as arguments. When I type
  27. ::     echo $something|sed 's/$this_pattern_var/$that_pattern_var/'
  28. :: 
  29. :: sed throws up on me. I've tried various combinations of quotation
  30. :: marks without success. Is there an easy way of doing this??
  31. :: 
  32. :
  33. :echo "$something" | sed 's/'"$this_pattern_var"'/'"$that_pattern_var"'/'
  34. :
  35. :The double quotes around the shell variables are only
  36. :needed if they contain any spaces (or tabs).
  37.  
  38. Untrue.
  39.  
  40. % set x='*' y='?'
  41. % sed s/$x/$y/
  42. s/: No such file or directory
  43.  
  44. This actually seems to work in a Bourne-shell, which tells 
  45. me that there's a difference in when retokenization happens,
  46. which was news to me.
  47.  
  48. Things like this drive people from either traditional shell
  49. into rc or perl, where you have more control over this.
  50.  
  51. You also will have a problem if your pattern contains delimiters,
  52. which is a bit of a hassle to work around.
  53.  
  54. Things like this drive people into perl, which doesn't have
  55. that problem either.
  56.  
  57. Of course, you can do these things in a traditional shell.
  58. But the variable quoting is a hassle, and the escaping of 
  59. the regexp delims is a hassle.  
  60.  
  61. --tom
  62. -- 
  63.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  64.  
  65. We should develop anti-satellite weapons because we could not have prevailed
  66. without them in 'Red Storm Rising'.  --Vice President Dan Quayle
  67.