Quantcast
Channel: Inserting a text between 2 patterns - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by cas for Inserting a text between 2 patterns

$
0
0

As chaos said, making the file a valid XML file and using xmlstarlet or similar xml processing tool (e.g. xsh) is the "right" answer...but if you just want to do simple templating then you would modify your input file to have easily found and replaceable tokens in the locations you want to be able to change. For example:

<controller-host>__CONTROLLER__</controller-host>
 <controller-port>__PORT__</controller-port>
<tier-name>__HOSTNAME__</tier-name>
<node-name>__NODENAME__</node-name>

Then you can use any of the usual tools (awk, sed, perl, ruby, whatever) to replace __CONTROLLER__ etc with the actual values you want them to have.

NOTE: it is very important to use strings that do not otherwise occur in the text - that's why i've used __ as part of the replacable strings, e.g. __PORT__ rather than just PORT.

This works for very simple templating needs - if you need to do more complicated templating, many languages have very sophisticated templating libraries - e.g. perl has Text::Template (and many others - it's a common problem that has inspired many solutions)


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>