home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / shell / 5466 < prev    next >
Encoding:
Internet Message Format  |  1993-01-23  |  1.8 KB

  1. Path: sparky!uunet!gossip.pyramid.com!pyramid!infmx!hartman
  2. From: hartman@informix.com (Robert Hartman)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: multiple sub's with 'sed'
  5. Message-ID: <1993Jan22.183334.17876@informix.com>
  6. Date: 22 Jan 93 18:33:34 GMT
  7. References: <19891@mindlink.bc.ca>
  8. Sender: news@informix.com (Usenet News)
  9. Organization: Informix Software, Inc.
  10. Lines: 41
  11.  
  12. In article <19891@mindlink.bc.ca> Ian_Staines@mindlink.bc.ca (Ian Staines) writes:
  13. >
  14. >How can I get 'sed' to do multiple substitutions?  I would like to replace
  15. >certain words in a text file with bold type.  I have tried the following:
  16. >
  17. >for WORD in ${LIST}
  18. >do
  19. >        BOLD="${bold}${WORD}${unbold}"
  20. >        sed "/s/${WORD}/${BOLD}/gw ${FILE}" ${FILE}
  21. >done
  22. >
  23. >The shell appears to keep ${FILE} intact within the script, and does not
  24. >updated it with each loop.  I only get the last substitution, not the entire
  25. >list.
  26. >
  27. >Is there a way to loop within sed?
  28.  
  29. I'm not quite sure why you have the ${FILE} within quotes.  Also, I
  30. don't know what the "w" is for after at the end of your substitution
  31. command; I've never used it.  You're only getting the last substitution
  32. because you aren't writing the earlier ones out to file.  This might
  33. work better:
  34.  
  35.     cat /dev/null > /tmp/sedscript.$USER
  36.  
  37.     for WORD in ${LIST}
  38.     do
  39.         BOLD="${bold}${WORD}${unbold}"
  40.         echo "s/${WORD}/${BOLD}/g" >> /tmp/sedscript.$USER
  41.     done
  42.     sed -f /tmp/sedscript.$USER ${FILE} > ${FILE}.sed
  43.  
  44.  
  45. This shell script fragment builds a sed script that includes all of the
  46. substitutions you want to make.  It then applies the sed script to
  47. the file and saves the output in a file with the .sed suffix.  It
  48. doesn't get rid of the sed script because you might want it around
  49. for debugging purposes.  However, it does overwrite any previous sed script
  50. created by you to avoid accumulating files in /tmp.
  51.  
  52. -r
  53.