home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- 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
- From: john@physiol.su.OZ.AU (John Mackin)
- Subject: Re: Sed with shell variables
- Message-ID: <1992Aug21.013604.5214@physiol.su.OZ.AU>
- Summary: Incorrect solution corrected
- Reply-To: John Mackin <john@civil.su.oz.au>
- Organization: Department of Physiology, University of Sydney, Australia
- References: <2701@nlsun1.oracle.nl> <1992Aug20.105737.29473@ccu1.aukuni.ac.nz>
- Date: Fri, 21 Aug 1992 01:36:04 GMT
- Lines: 56
-
- In article <1992Aug20.105737.29473@ccu1.aukuni.ac.nz>,
- ecmtwhk@ccu1.aukuni.ac.nz (Thomas Koenig) writes:
-
- > rgasch@nl.oracle.com (Robert Gasch) writes:
- >
- >>The problem I ran into is that I can't get shell variables to
- >>work as arguments. When I type
- >> echo $something|sed 's/$this_pattern_var/$that_pattern_var/'
- >
- >>sed throws up on me. I've tried various combinations of quotation
- >>marks without success. Is there an easy way of doing this??
- >
- > echo $something | sed -e s/$this_pattern/$that_pattern/
-
- Unfortunately, Thomas's solution is wrong. Since Robert doesn't
- say what shell he is concerned about, we can only suppose that he
- wants a solution for a `conventional' shell, such as sh or csh
- (and not, for example, rc, where Thomas's solution would be right,
- but for the wrong reasons). These shells are macro languages that
- re-scan their arguments for delimiters (usually, white space)
- after variable substitution. So the solution presented above
- will only work as long as the values of the two pattern variables
- don't contain any whitespace (space or tab characters).
-
- Next time, Robert, please do say what shell you are asking about.
- I hope, though, that the same solution is correct in this instance
- for both sh and csh. I know it is correct in sh.
-
- Worked example (": mod; " is the prompt):
-
- #
- # Set variables
- #
- : mod; from='from pat'
- : mod; to='to pat'
-
- #
- # Demonstrate that Thomas's idea does not suffice
- #
- : mod; echo "from pat to bill it's all a thrill" | sed s/$from/$to/
- command garbled: s/from
-
- #
- # Give the correct solution
- #
- : mod; echo "from pat to bill it's all a thrill" | sed "s/$from/$to/"
- to pat to bill it's all a thrill
-
- That's the way double-quotes work in sh (and, I think, in csh).
- Great, isn't it? Change to rc now, while you're still impressionable
- (you _must_ be impressionable, I hope, if you didn't know how to
- do this), and reap the benefit of _only one kind of quoting_!!!
-
- --
- John Mackin <john@civil.su.oz.au>
- case '\7': *((char *)(384*1024L+062)) = 2; /* beep */
-