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

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!mips!mips!munnari.oz.au!metro!physiol.su.OZ.AU!john
  3. From: john@physiol.su.OZ.AU (John Mackin)
  4. Subject: Re: Sed with shell variables
  5. Message-ID: <1992Aug21.013604.5214@physiol.su.OZ.AU>
  6. Summary: Incorrect solution corrected
  7. Reply-To: John Mackin <john@civil.su.oz.au>
  8. Organization: Department of Physiology, University of Sydney, Australia
  9. References: <2701@nlsun1.oracle.nl> <1992Aug20.105737.29473@ccu1.aukuni.ac.nz>
  10. Date: Fri, 21 Aug 1992 01:36:04 GMT
  11. Lines: 56
  12.  
  13. In article <1992Aug20.105737.29473@ccu1.aukuni.ac.nz>,
  14.     ecmtwhk@ccu1.aukuni.ac.nz (Thomas Koenig) writes:
  15.  
  16. > rgasch@nl.oracle.com (Robert Gasch) writes:
  17. >
  18. >>The problem I ran into is that I can't get shell variables to
  19. >>work as arguments. When I type
  20. >>    echo $something|sed 's/$this_pattern_var/$that_pattern_var/'
  21. >
  22. >>sed throws up on me. I've tried various combinations of quotation
  23. >>marks without success. Is there an easy way of doing this??
  24. >
  25. > echo $something | sed -e s/$this_pattern/$that_pattern/
  26.  
  27. Unfortunately, Thomas's solution is wrong.  Since Robert doesn't
  28. say what shell he is concerned about, we can only suppose that he
  29. wants a solution for a `conventional' shell, such as sh or csh
  30. (and not, for example, rc, where Thomas's solution would be right,
  31. but for the wrong reasons).  These shells are macro languages that
  32. re-scan their arguments for delimiters (usually, white space)
  33. after variable substitution.  So the solution presented above
  34. will only work as long as the values of the two pattern variables
  35. don't contain any whitespace (space or tab characters).
  36.  
  37. Next time, Robert, please do say what shell you are asking about.
  38. I hope, though, that the same solution is correct in this instance
  39. for both sh and csh.  I know it is correct in sh.
  40.  
  41. Worked example (": mod; " is the prompt):
  42.  
  43. #
  44. # Set variables
  45. #
  46. : mod; from='from pat'
  47. : mod; to='to pat'
  48.  
  49. #
  50. # Demonstrate that Thomas's idea does not suffice
  51. #
  52. : mod; echo "from pat to bill it's all a thrill" | sed s/$from/$to/
  53. command garbled: s/from
  54.  
  55. #
  56. # Give the correct solution
  57. #
  58. : mod; echo "from pat to bill it's all a thrill" | sed "s/$from/$to/"
  59. to pat to bill it's all a thrill
  60.  
  61. That's the way double-quotes work in sh (and, I think, in csh).
  62. Great, isn't it?  Change to rc now, while you're still impressionable
  63. (you _must_ be impressionable, I hope, if you didn't know how to
  64. do this), and reap the benefit of _only one kind of quoting_!!!
  65.  
  66. -- 
  67. John Mackin <john@civil.su.oz.au>
  68.         case '\7': *((char *)(384*1024L+062)) = 2;      /* beep */
  69.