home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.questions:14695 comp.unix.shell:5069
- Path: sparky!uunet!spool.mu.edu!uwm.edu!cs.utexas.edu!asuvax!ukma!cs.widener.edu!dsinc!phent!kkress
- From: kkress@phent.UUCP (Kenneth R. Kress)
- Newsgroups: comp.unix.questions,comp.unix.shell
- Distribution: world
- Subject: Re: Substituting using sed, ...
- Keywords: sed m4 substitutions quoted strings
- Message-ID: <9212144462@phent.UUCP>
- Organization: That's Entertainment BBS
- Date: Mon, 14 Dec 92 23:50:27 EST
- Lines: 64
-
- Bruce Momjian writes:
- > Subject: Substituting using sed, skipping quoted strings
- > Date: Mon, 14 Dec 1992 04:24:22 GMT
- >
- > Here is a sample file:
- >
- > acaX csacacXcascsc
- > "cscaXcacsXcs" csaXc "X" caX
- > Xaca Xcaca ca "caXcas" ccaX cacs
- >
- > I want to use sed to replace X with another character, but only if the X
- > does not appear within quotes. The quoted strings do not extend across
- > new-lines.
- >
- > --
- > Bruce Momjian | 830 Blythe Avenue
- > root%candle.uucp@bts.com | Drexel Hill, Pennsylvania 19026
- > + If your life is a hard drive, | (215) 353-9879(w)
- > + Christ can be your backup. | (215) 853-3000(h)
-
- Howdy neighbor,
- That is one cruel and inhuman torturous problem. I like to solve
- things first, no matter how crude the solution, and then tidy up, but this
- was so ugly, I'll offer it in the hopes that someone else will show both of
- us a "nice" solution using sed. Here goes.
-
- test.sed data2 produces:
-
- acaY csacacYcascsc
- "cscaXcacsXcs" csaYc "X" caY
- Yaca Ycaca ca "caXcas" ccaY cacs
-
- cat test.sed produces:
- sed 's/X/ X /g' |
- sed 's/\"/`/' |
- sed 's/\"/~/' |
- sed 's/\"/`/' |
- sed 's/\"/~/' |
- sed 's/\"/`/' |
- sed 's/\"/~/' |
- sed 's/\"/`/' |
- sed 's/\"/~/' |
- sed 's/`/``/g
- s/~/~~/g' |
- tr "~" "'" > $$tmp
- m4 test.m4 $$tmp > $$tmpa
- sed 's/ X /X/g
- s/ Y /Y/g
- s/`/\"/g' $$tmpa |
- sed "s/\'/\"/g"
- # clean exit
- rm $$tm*
- exit 0
-
- Let it's hideousness inspire others to respond.
-
- Ken.
-
- ===================================================================
- Ken Kress, Glenside, PA * dsinc!phent!kkress
- Daring ideas are like chessmen moved forward;
- they may be beaten, but they may start a winning game.
- Johann Wolfgang von Goethe
- -------------------------------------------------------------------
-