Search a template file and replace with input

Hi I have a CommonTemplateStop.template file . Inside the file i need to replace the variables DepName and CompInsName with the values(Trade and TradeIns) specified in the script. I have written the below .sh script in linux server which will read the .template file and has to replace the 2 parameters DepName and CompInsName with the values specified in the script .When i call the script below i get the below error:

sed: -e expression #1, char 9: missing command
sed: -e expression #1, char 13: missing command

Am quite a novice in unix scripting. Any help is appreciated.

#!/bin/bash
DepName=Trade
CompInsName=TradeIns
templatefile=/tmp/sam/CommonTemplateStop.template
cat $templatefile|sed -e "/DepName/"
cat $templatefile|sed -e "/CompInsName/"

Template file given below :

<?xml version="1.0" encoding="UTF-8" ?>
<ruleBase>
    <version>4.6.0</version>
    <name><![CDATA[CommonTemplateStop]]></name>
    <schedule></schedule>
    <author>sdutta on host SDuttaDT(192.168.112.134) at 15:49 Wed, Jul 29, 2015</author>
    <lastModification>sdutta on host SDuttaDT(192.168.112.134) at 11:46 Tue, Aug 11, 2015</lastModification>
    <comment><![CDATA[]]></comment>
    <rule>
        <name><![CDATA[COM.TIBCO.admin.TRA:getComponentInstanceStatus(Component Instance=${External.CompInsName}, Deployment=${External.DepName}):15]]></name>
        <schedule>AlertWindow</schedule>
        <overRuling>0</overRuling>
        <dataSource>
            <microAgentName><![CDATA[COM.TIBCO.admin.TRA]]></microAgentName>
            <methodName>getComponentInstanceStatus</methodName>
            <dataElement name="Deployment">
                <dataObject class="java.lang.String" ><![CDATA[${External.DepName}]]></dataObject>
            </dataElement>
            <dataElement name="Component Instance">
                <dataObject class="java.lang.String" ><![CDATA[${External.CompInsName}]]></dataObject>
            </dataElement>
            <interval>15000</interval>
        </dataSource>
</rule>
</rulebase>

where is the $ for the variable? e.g.

 /$DepName/
1 Like

The error msgs are correct - there's no sed command, just addresses. Try

sed "s/CompInsName/$CompInsName/;s/DepName/$DepName/g" file

but make sure the variables don't have "/" in them.

1 Like

Examples of commands that sed complains about, would be:

sed -n "/DepName/p" "$templatefile"
sed  "/DepName/!d" "$templatefile"

Hi I got the desired output file. Thanks ALL :slight_smile:

#!/bin/bash
CompInsName=$1
DepName=$2
templatefile=/usr/tmp/CommonTemplateStop.template
sed "s/CompInsName/$CompInsName/;s/DepName/$DepName/g" $templatefile > /usr/tmp/autoconfig/CommonTemplateStop.hrb
chmod 774 /usr/tmp/autoconfig/CommonTemplateStop.hrb

Are you sure you need the 774 permission on that file?

Well Rudi i gave that permission just to make sure that the final file is readable and executable. This file shall be invoked by a monitoring application called TIBCO Hawk and the rules implemented.

Do you suggest otherwise?

Does it really need to be executable? Being a config file, I'd guess it needed read permission (and mayhap write for the owner).

Thanks All. Its resolved :slight_smile:

Samrat