home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.shell:5075 comp.unix.questions:14700
- Path: sparky!uunet!mcsun!Germany.EU.net!sbsvax!mpi-sb.mpg.de!uwe
- From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
- Newsgroups: comp.unix.shell,comp.unix.questions
- Subject: Re: Substituting using sed, skipping quoted strings
- Message-ID: <23330@sbsvax.cs.uni-sb.de>
- Date: 15 Dec 92 14:29:07 GMT
- References: <1992Dec14.042422.22897@candle.uucp>
- Sender: news@sbsvax.cs.uni-sb.de
- Reply-To: uwe@mpi-sb.mpg.de
- Followup-To: comp.unix.shell
- Organization: Max-Planck-Institut fuer Informatik
- Lines: 36
-
- In article <1992Dec14.042422.22897@candle.uucp>, root@candle.uucp
- (Bruce Momjian) writes:
- > 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.
-
- That's easy:
-
- #!/bin/sh
- sed -e '
- s/^/\
- /
- : a
- s/\(\n[^"]*\)X/\1Y/
- t a
- /\n.*".*"/ {
- s/\n\([^"]*\)"\([^"]*\)"/\1"\2"\
- /
- t a
- }
- s/\n//
- ' ${1+"$@"}
-
- The extension to quoted strings that do extend across newlines is left
- to the reader :-)
-
- --
- Uwe Waldmann, Max-Planck-Institut fuer Informatik
- Im Stadtwald, D-W6600 Saarbruecken 1, Germany
- Phone: +49 681 302-5431, Fax: +49 681 302-5401, E-Mail: uwe@mpi-sb.mpg.de
-