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


Groups > comp.programming > #4123

Re: Escaping of line breaks for text replacement

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From dave.gma+news002@googlemail.com.invalid (Dave Sines)
Newsgroups comp.unix.programmer, comp.programming
Subject Re: Escaping of line breaks for text replacement
Followup-To comp.unix.programmer
Date Sun, 02 Mar 2014 16:52:11 +0000
Organization A noiseless patient Spider
Lines 64
Message-ID <r64cuaxu32.ln2@perseus.wenlock-data.co.uk> (permalink)
References <bngoc5F196mU1@mid.individual.net>
Injection-Info mx05.eternal-september.org; posting-host="c8663c86a76665c1575f87bf2e027986"; logging-data="2097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dOynywlNCtARtsEmFB9V+"
User-Agent tin/2.2.0-20131224 ("Lochindaal") (UNIX) (Linux/3.0.0-1 (i686))
Cancel-Lock sha1:66VXoElbI1/O9jezDhrOPh6S64s=
Xref csiph.com comp.unix.programmer:5151 comp.programming:4123

Cross-posted to 2 groups.

Followups directed to: comp.unix.programmer

Show key headers only | View raw


[ Followup-To: set to comp.unix.programmer ]

Markus Elfring <Markus.Elfring@web.de> wrote:
> Hello,
> 
> I get the following result if I try out such a command on my
> openSUSE system.
> 
> elfring@Sonne:~> $SHELL --version && sed --version &&
> LINE='echo ---' && $LINE && DRAFT='template1.txt' && cat $DRAFT &&
> $LINE && INPUT='replacement1.txt' && cat $INPUT && XY=$(< $INPUT) &&
> $LINE && LANG=C sed "s/# placeholder/$XY/" $DRAFT

> GNU bash, Version 4.2.45(1)-release (x86_64-suse-linux-gnu)
>   ...
> sed (GNU sed) 4.2.2
>   ...
> ---
> +++++++++++++
> # placeholder
> *************
> ---
> ABC
> XYZ
> ---
> sed: -e expression #1, char 19: unterminated `s' command

DRAFT=template1.txt
REPLACEMENT=replacement1.txt

sed '/^# placeholder/{
r '"$REPLACEMENT"'
d
}' "$DRAFT"

Note the change from single to double quotes and back -- the script is
composed of three adjacent strings which will be concatenated by the
shell to form a single argument to sed.

> 
> I guess that the shown error message is an indication that I should
> add a bit of escaping to the content of the variable "XY" which I
> inserted previously. I have tried to achieve this with shell pattern
> substitution.
> 
> elfring@Sonne:~> XY=$(< $INPUT) && XY=${XY//\\\n/./} && echo $XY
> ABC XYZ
> 
> I wonder why I do not get this approach to work so far with some
> additional backslashes here. How should newlines be escaped for
> this purpose?

Use the shell's backslash-escape expansion ($'what\never').

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.

  XY=$(< "$REPLACEMENT" )
  XY=${XY//$'\n'/$'\\\n'}
  XY=${XY//\//\\\/}

  sed "s/# placeholder/$XY/" "$DRAFT"

Back to comp.programming | 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 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 18:11 +0000
  Re: Escaping of line breaks for text replacement Siri Cruz <chine.bleu@yahoo.com> - 2014-03-07 02:47 -0800

csiph-web