I generate the configuration on the fly, I need to replace the {{ namespace.default_ttl }} entry with a block of several lines:

 stanza { storage a engine n ttl c } 

The block rolls in the adjacent file and can not be set manually with \n instead of line breaks.

How to do this? There are sed and awk available, I would like to do without perl.

  • sed. write carriage translations as \n . maybe backslash will need to double \\n - Mike
  • The fact is that @Mike, that I work with a ready text, in which the carriage translations are in the usual form - etki
  • But where will you get the text for the sed command? I thought you would write her in what script with your hands and that's it - Mike
  • @Mike from a nearby file that should be able to change from release to release - etki

1 answer 1

test file:

 $ cat file 123{{ namespace.default_ttl }}456 

we apply to it a small program in the internal language of the sed program:

 $ sed -i 's/{{ namespace.default_ttl }}/\nstanza {\n\tstorage a\n\tengine n\n\tttl c\n}/' file 

we get in the file what is required:

 $ cat file 123 stanza { storage a engine n ttl c } 456 

addition

if the text for the substitution is in a separate file:

 $ cat pattern stanza { storage a engine n ttl c } 

then it should only be read, for example, by the cat program (using the e modifier from the s command of the sed program, which allows the shell to form the command in the pattern space and replace the pattern space with its output). but if the required substring is “framed” with some other text, then two “approaches” will be required - first, separate the substring from the “framing” text with line breaks:

 $ sed -i 's/{{ namespace.default_ttl }}/\n&\n/' file 

and then replace the desired substring with the contents of the pattern file:

 $ sed -i 's/{{ namespace.default_ttl }}/cat pattern/e' file 
  • clarified the question` - etki
  • @Etki, added the answer. - aleksandr barakin
  • I'm afraid sed still swears at the hastebin.com/towatilesa.bash pattern of problems -> sed: -e expression #1, char 51: unterminated `s' command . There are no slashes in / etc / hosts, when replacing the control character with @ the same situation. - etki
  • @Etki, $(команда) is not needed there. just a команда - aleksandr barakin
  • Yeah, I thought it was a typo (plus everything got lost on the first attempt because of the slashes on the way, everything works with the replacement of the control character) - etki