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

  1. Xref: sparky comp.unix.shell:5075 comp.unix.questions:14700
  2. Path: sparky!uunet!mcsun!Germany.EU.net!sbsvax!mpi-sb.mpg.de!uwe
  3. From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
  4. Newsgroups: comp.unix.shell,comp.unix.questions
  5. Subject: Re: Substituting using sed, skipping quoted strings
  6. Message-ID: <23330@sbsvax.cs.uni-sb.de>
  7. Date: 15 Dec 92 14:29:07 GMT
  8. References: <1992Dec14.042422.22897@candle.uucp>
  9. Sender: news@sbsvax.cs.uni-sb.de
  10. Reply-To: uwe@mpi-sb.mpg.de
  11. Followup-To: comp.unix.shell
  12. Organization: Max-Planck-Institut fuer Informatik
  13. Lines: 36
  14.  
  15. In article <1992Dec14.042422.22897@candle.uucp>, root@candle.uucp
  16. (Bruce Momjian) writes:
  17. > Here is a sample file:
  18. >     acaX csacacXcascsc  
  19. >     "cscaXcacsXcs"  csaXc "X" caX
  20. >     Xaca Xcaca  ca "caXcas"  ccaX cacs
  21. > I want to use sed to replace X with another character, but only if the X
  22. > does not appear within quotes.  The quoted strings do not extend across
  23. > new-lines.
  24.  
  25. That's easy:
  26.  
  27. #!/bin/sh
  28. sed -e '
  29. s/^/\
  30. /
  31. : a
  32. s/\(\n[^"]*\)X/\1Y/
  33. t a
  34. /\n.*".*"/ {
  35. s/\n\([^"]*\)"\([^"]*\)"/\1"\2"\
  36. /
  37. t a
  38. }
  39. s/\n//
  40. ' ${1+"$@"}
  41.  
  42. The extension to quoted strings that do extend across newlines is left
  43. to the reader :-)
  44.  
  45. -- 
  46. Uwe Waldmann, Max-Planck-Institut fuer Informatik
  47. Im Stadtwald, D-W6600 Saarbruecken 1, Germany
  48. Phone: +49 681 302-5431, Fax: +49 681 302-5401, E-Mail: uwe@mpi-sb.mpg.de
  49.