home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gossip.pyramid.com!pyramid!infmx!hartman
- From: hartman@informix.com (Robert Hartman)
- Newsgroups: comp.unix.shell
- Subject: Re: multiple sub's with 'sed'
- Message-ID: <1993Jan22.183334.17876@informix.com>
- Date: 22 Jan 93 18:33:34 GMT
- References: <19891@mindlink.bc.ca>
- Sender: news@informix.com (Usenet News)
- Organization: Informix Software, Inc.
- Lines: 41
-
- In article <19891@mindlink.bc.ca> Ian_Staines@mindlink.bc.ca (Ian Staines) writes:
- >
- >How can I get 'sed' to do multiple substitutions? I would like to replace
- >certain words in a text file with bold type. I have tried the following:
- >
- >for WORD in ${LIST}
- >do
- > BOLD="${bold}${WORD}${unbold}"
- > sed "/s/${WORD}/${BOLD}/gw ${FILE}" ${FILE}
- >done
- >
- >The shell appears to keep ${FILE} intact within the script, and does not
- >updated it with each loop. I only get the last substitution, not the entire
- >list.
- >
- >Is there a way to loop within sed?
-
- I'm not quite sure why you have the ${FILE} within quotes. Also, I
- don't know what the "w" is for after at the end of your substitution
- command; I've never used it. You're only getting the last substitution
- because you aren't writing the earlier ones out to file. This might
- work better:
-
- cat /dev/null > /tmp/sedscript.$USER
-
- for WORD in ${LIST}
- do
- BOLD="${bold}${WORD}${unbold}"
- echo "s/${WORD}/${BOLD}/g" >> /tmp/sedscript.$USER
- done
- sed -f /tmp/sedscript.$USER ${FILE} > ${FILE}.sed
-
-
- This shell script fragment builds a sed script that includes all of the
- substitutions you want to make. It then applies the sed script to
- the file and saves the output in a file with the .sed suffix. It
- doesn't get rid of the sed script because you might want it around
- for debugging purposes. However, it does overwrite any previous sed script
- created by you to avoid accumulating files in /tmp.
-
- -r
-