{"id":3186,"date":"2010-04-16T11:57:03","date_gmt":"2010-04-16T11:57:03","guid":{"rendered":""},"modified":"2026-04-18T22:54:23","modified_gmt":"2026-04-18T22:54:23","slug":"bash-insert-content-of-one-file-into-another-file","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=3186","title":{"rendered":"Bash &#8211; Insert content of one file into another file"},"content":{"rendered":"<p>Had to insert a XML-snippet into another XML-file at a given location in a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bash\">Bash-script<\/a>. Reading the snippet into a variable is quite easy, just by using <a href=\"http:\/\/linux.about.com\/od\/commands\/l\/blcmdl1_cat.htm\">cat<\/a>. The trouble was how to find the tag to replace most efficiently, then replace it with the xml-snippet. Learned that reading the file content using stdin\/stdout in a <a href=\"http:\/\/en.kioskea.net\/faq\/1757-how-to-read-a-file-line-by-line\">bash-script<\/a>  can cause side effects not easy to figure out, so that was no option. Instead I first tried to use <a href=\"http:\/\/linux.about.com\/od\/commands\/l\/blcmdl1_sed.htm\">sed<\/a>, but sed had problems with the fact that the content of the snippet is XML-code. So instead with good help from <a href=\"http:\/\/www.linkedin.com\/pub\/geir-sjurseth\/3\/114\/575\">Geir<\/a>  we ended up using Perl, which made it all very simple &#8211; everything done in just 2 lines of code.<\/p>\n<p>First it searches for &lt;!&#8211; END jaas &#8211;&gt; then replaces the string with the content of the snippet variable and lt;!&#8211; END jaas &#8211;&gt;<br \/>\n<code><br \/>\nvalue=$(cat jaas_snippet.xml)<br \/>\nperl -pi -e \"s#&lt;!--\\sEND\\sjaas\\s--&gt;#${value}\\n\\t\\t&lt;!-- END jaas --&gt;#\" jaas.xml<br \/>\n<\/code><br \/>\nComments:<\/p>\n<ul>\n<li>Using # instead of ` &#8211; while struggling with XML in the variable $value <\/li>\n<li>\\s = whitespace, like \\n is newline and \\t is tab<\/li>\n<li>Double quote&#8217;s so that perl uses the <a href=\"http:\/\/www.unix.com\/shell-programming-scripting\/49369-replace-value-variable-using-sed.html\">value of the variable<\/a>, and not just reads it as plain text<\/li>\n<li>In this case we expect only one occurrence of the end-tag, so we have skipped the \/g parameter at the end of the search-string. <a href=\"http:\/\/www.regular-expressions.info\/perl.html\">\/g meaning global<\/a>, compared to for instance the usage described in my <a href=\"http:\/\/blog.workplace.no\/www\/wpc\/WPCblog.nsf\/d6plinks\/RHEM-79STGR\">previous blog post<\/a> at WPC.<\/li>\n<\/ul>\n<p>Perl is power!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had to insert a XML-snippet into another XML-file at a given location in a Bash-script. Reading the snippet into a variable is quite easy, just by using cat. The trouble was how to find the tag to replace most efficiently, then replace it with the xml-snippet. Learned that reading the file content using stdin\/stdout in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-3186","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3186"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3186\/revisions"}],"predecessor-version":[{"id":14417,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3186\/revisions\/14417"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}