Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #4124
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.unix.programmer, comp.programming |
| Subject | Re: Escaping of line breaks for text replacement |
| Followup-To | comp.unix.programmer |
| Date | 2014-03-02 18:11 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <20140302095342.219@kylheku.com> (permalink) |
| References | <bngoc5F196mU1@mid.individual.net> |
Cross-posted to 2 groups.
Followups directed to: comp.unix.programmer
["Followup-To:" header set to comp.unix.programmer.] On 2014-03-02, 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 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 <<EOF $(cat template.txt) EOF" my terminal is xterm the number of lines is 44 the number of colums is 80 You can have newlines in the variables that are substituted. If you put the here-doc prologue and epilogue "cat <<EOF" and "EOF" the template itself, then it's just a straight eval "$(cat template.txt)". Remember to escape dollar signs in the here doc. It has to follow the rules for a here doc. If you ever do use sed for variable substitution have a clear delimiter on the variables. For instance, call it @REPLACEMENT@, not replacement, for reasons which should be obvious.
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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