Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #5172

Re: Escaping of line breaks for text replacement

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.unix.programmer
Subject Re: Escaping of line breaks for text replacement
Date 2014-03-03 17:44 +0000
Organization Aioe.org NNTP Server
Message-ID <20140303093617.141@kylheku.com> (permalink)
References <bngoc5F196mU1@mid.individual.net> <r64cuaxu32.ln2@perseus.wenlock-data.co.uk> <20140302095200.408@kylheku.com> <20140302122629.500@kylheku.com> <8imeuaxmv1.ln2@perseus.wenlock-data.co.uk>

Show all headers | View raw


On 2014-03-03, Dave Sines <dave.gma+news002@googlemail.com.invalid> wrote:
> Kaz Kylheku <kaz@kylheku.com> wrote:
>> On 2014-03-02, Kaz Kylheku <kaz@kylheku.com> wrote:
>>> On 2014-03-02, Dave Sines <dave.gma+news002@googlemail.com.invalid>
>>> wrote:
>>>> It's not just newlines, any characters in the replacement text which
>>>> match sed's s command delimiter ('/' by default) will also need to
>>>> be escaped.
>>>
>>> Almost right:
>
> I have a slightly unhelpful tendency to assume anyone doing "s/foo/$BAR/"
> is trying to manipulate pathnames.
>
>>>
>>> "any characters that are special in sed's regex syntax need to
>>> be escaped."
>> 
>> Actually that is not correct either,
>
> Yes it is.
>
>> because this appears in the
>> replacement side of the regex. But there is some syntax there too
>> like positional references and & which have to be escaped.
>
> The back-references can be considered part of the regular expression syntax.

No, not really. Conceptually, the POSIX regex API returns an array of matches,
and this replacement side notation in the tools is a language for indexing into
that array to extract the matches into the substitution template.

These are not back-references.  Back-references are something which
occurs in the pattern-matching language itself (possibly rendering it non-regular).

A back-reference asserts "I'm a subexpression which matches the same thing as
another subexpression did earlier".

Confusion stems from backreferences in the pattern having the same syntax as
the replacement-side positional references, e.g. \1.

In any case, if you're interpolating strings into the replacement side of a s/a/b/
command, you do not have to escape the regex operators like ? or +. You have
to escape the replacement-side operators.

  $ sed -e 's/a/*/'
  a
  *

See? * is just itself; so we don't need \* to insert a literal *. Different language.

Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 14:00 +0100
  Re: Escaping of line breaks for text replacement jt@toerring.de (Jens Thoms Toerring) - 2014-03-02 15:49 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 08:38 +0100
  Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-02 16:52 +0000
    Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 17:53 +0000
      Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:27 +0000
        Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 16:17 +0000
          Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-03 17:44 +0000
            Re: Escaping of line breaks for text replacement Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2014-03-04 13:30 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 20:50 +0100
      Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 16:52 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 09:24 +0100
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 12:55 +0100
      Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 17:02 +0000
        Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-06 12:02 +0100
          Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-06 18:51 +0000
            Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-06 20:48 +0100
              Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-06 22:59 +0000
                Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-07 06:38 +0000
                Re: Escaping of line breaks for text replacement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-07 13:33 +0000
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 15:08 +0000
                Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-07 11:23 +0100
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 14:43 +0000
                Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-07 17:23 +0100
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 19:53 +0000
                Bash 4.3.0 escape expansion (was: Re: Escaping of line breaks for text replacement) dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-08 12:26 +0000
                Re: Bash 4.3.0 escape expansion (was: Re: Escaping of line breaks for text replacement) Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 20:05 +0000
                Re: Bash 4.3.0 escape expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-09 03:48 +0000
                Re: Bash 4.3.0 escape expansion dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-09 13:21 +0000
  Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 18:11 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 20:08 +0100
      Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:35 +0000
        Re: Which content should be escaped? Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 10:30 +0100
          Re: Which content should be escaped? dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-04 14:04 +0000
            Re: Which content should be escaped? Kaz Kylheku <kaz@kylheku.com> - 2014-03-04 17:32 +0000
  Re: Escaping of line breaks for text replacement Siri Cruz <chine.bleu@yahoo.com> - 2014-03-07 02:47 -0800

csiph-web