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


Groups > comp.programming > #4122

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!fu-berlin.de!uni-berlin.de!not-for-mail
From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.unix.programmer, comp.programming
Subject Re: Escaping of line breaks for text replacement
Date 2 Mar 2014 15:49:52 GMT
Organization Freie Universitaet Berlin
Lines 49
Message-ID <bnh28uF33gmU1@mid.uni-berlin.de> (permalink)
References <bngoc5F196mU1@mid.individual.net>
X-Trace news.uni-berlin.de Nt0MOC2z5rKUrIt0yKTAQAMiH2oGqAUN30ZbuSKWL3ReDh
X-Orig-Path not-for-mail
User-Agent tin/2.1.1-20120623 ("Mulindry") (UNIX) (Linux/3.2.0-4-amd64 (x86_64))
Xref csiph.com comp.unix.programmer:5150 comp.programming:4122

Cross-posted to 2 groups.

Show key headers only | View raw


In 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


> 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.

Yes the problem is the linefeed in the replacement you pass to sed
in the end. You need to reolace the linefeeds read from the "re-
placements1.txt" file by '\\n' for sed to start to like them. One
way to do it would be e.g.

$SHELL --version && sed --version && LINE='echo ---' && $LINE &&
DRAFT='template1.txt' && cat $DRAFT && $LINE && INPUT='replacement1.txt' &&
cat $INPUT && XY=`sed ':a;N;$!ba;s/\n/\\\n/g' $INPUT` && $LINE && LANG=C sed
"s/# placeholder/$XY/" $DRAFT

With

XY=`sed ':a;N;$!ba;s/\n/\\\n/g' $INPUT`

you set XY to the files contents with linefeeds replaced by
'\\n' which is then acceptable to sed.

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

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