Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!news.stack.nl!aioe.org!.POSTED!not-for-mail From: Kaz Kylheku Newsgroups: comp.unix.programmer,comp.programming Subject: Re: Escaping of line breaks for text replacement Followup-To: comp.unix.programmer Date: Sun, 2 Mar 2014 18:11:59 +0000 (UTC) Organization: Aioe.org NNTP Server Lines: 58 Message-ID: <20140302095342.219@kylheku.com> References: NNTP-Posting-Host: X+c6YNb3AaWMPA3YfA4opg.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: slrn/pre1.0.0-18 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.unix.programmer:5153 comp.programming:4124 ["Followup-To:" header set to comp.unix.programmer.] On 2014-03-02, Markus Elfring 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 Unix already has a mechanism for interpolating variables into multi-line text, called a "here document". Here, it behooves us to use it, rather than reach for sed tricks to do variable substitution. Live demo: $ cat template.txt my terminal is $TERM the number of lines is $LINES the number of colums is $COLUMNS Now: dynamically wrap the here-doc syntax around the contents of template.txt and evaluate it: $ eval "cat <