home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / question / 14695 < prev    next >
Encoding:
Internet Message Format  |  1992-12-16  |  2.2 KB

  1. Xref: sparky comp.unix.questions:14695 comp.unix.shell:5069
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!cs.utexas.edu!asuvax!ukma!cs.widener.edu!dsinc!phent!kkress
  3. From: kkress@phent.UUCP (Kenneth R. Kress)
  4. Newsgroups: comp.unix.questions,comp.unix.shell
  5. Distribution: world
  6. Subject: Re: Substituting using sed, ...
  7. Keywords: sed m4 substitutions quoted strings
  8. Message-ID: <9212144462@phent.UUCP>
  9. Organization: That's Entertainment BBS
  10. Date: Mon, 14 Dec 92 23:50:27 EST
  11. Lines: 64
  12.  
  13. Bruce Momjian writes:
  14. > Subject: Substituting using sed, skipping quoted strings
  15. > Date: Mon, 14 Dec 1992 04:24:22 GMT
  16. > Here is a sample file:
  17. >     acaX csacacXcascsc  
  18. >     "cscaXcacsXcs"  csaXc "X" caX
  19. >     Xaca Xcaca  ca "caXcas"  ccaX cacs
  20. > I want to use sed to replace X with another character, but only if the X
  21. > does not appear within quotes.  The quoted strings do not extend across
  22. > new-lines.
  23. > -- 
  24. > Bruce Momjian                          |  830 Blythe Avenue
  25. > root%candle.uucp@bts.com               |  Drexel Hill, Pennsylvania 19026 
  26. >   +  If your life is a hard drive,     |  (215) 353-9879(w) 
  27. >   +  Christ can be your backup.        |  (215) 853-3000(h)
  28.  
  29. Howdy neighbor,
  30.     That is one cruel and inhuman torturous problem. I like to solve
  31. things first, no matter how crude the solution, and then tidy up, but this
  32. was so ugly, I'll offer it in the hopes that someone else will show both of
  33. us a "nice" solution using sed.   Here goes.
  34.  
  35. test.sed data2 produces:
  36.  
  37.     acaY csacacYcascsc  
  38.     "cscaXcacsXcs"  csaYc "X" caY
  39.     Yaca Ycaca  ca "caXcas"  ccaY cacs
  40.  
  41. cat test.sed produces:
  42. sed 's/X/ X /g'     |
  43. sed 's/\"/`/'        |
  44. sed 's/\"/~/'        |
  45. sed 's/\"/`/'        |
  46. sed 's/\"/~/'        |
  47. sed 's/\"/`/'        |
  48. sed 's/\"/~/'        |
  49. sed 's/\"/`/'        |
  50. sed 's/\"/~/'        |
  51. sed    's/`/``/g
  52.     s/~/~~/g'    |
  53. tr "~" "'" > $$tmp
  54. m4 test.m4 $$tmp    > $$tmpa
  55. sed    's/ X /X/g
  56.     s/ Y /Y/g
  57.     s/`/\"/g' $$tmpa |
  58. sed "s/\'/\"/g"
  59. # clean exit
  60. rm $$tm*
  61. exit 0
  62.  
  63. Let it's hideousness inspire others to respond.
  64.  
  65.                 Ken.
  66.  
  67. ===================================================================
  68. Ken Kress, Glenside, PA * dsinc!phent!kkress 
  69.     Daring ideas are like chessmen moved forward;
  70.     they may be beaten, but they may start a winning game.
  71.                     Johann Wolfgang von Goethe
  72. -------------------------------------------------------------------
  73.