Visit Our UNIX and Linux User Community


Search a template file and replace with input


 
Thread Tools Search this Thread
Operating Systems Linux Search a template file and replace with input
# 1  
Old 08-11-2015
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:
Code:
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.

Code:
#!/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 :

Code:
<?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>

# 2  
Old 08-11-2015
where is the $ for the variable? e.g.
Code:
 /$DepName/


Last edited by vbe; 08-11-2015 at 01:40 PM.. Reason: forgot [/code]...
This User Gave Thanks to vbe For This Post:
# 3  
Old 08-11-2015
The error msgs are correct - there's no sed command, just addresses. Try
Code:
sed "s/CompInsName/$CompInsName/;s/DepName/$DepName/g" file

but make sure the variables don't have "/" in them.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-11-2015
Examples of commands that sed complains about, would be:
Code:
sed -n "/DepName/p" "$templatefile"
sed  "/DepName/!d" "$templatefile"


Last edited by Scrutinizer; 08-11-2015 at 04:00 PM..
# 5  
Old 08-12-2015
Hi I got the desired output file. Thanks ALL Smilie

Code:
#!/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


Last edited by samrat dutta; 08-12-2015 at 06:27 PM..
# 6  
Old 08-13-2015
Are you sure you need the 774 permission on that file?
# 7  
Old 08-14-2015
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?

Previous Thread | Next Thread
Test Your Knowledge in Computers #131
Difficulty: Easy
UNIX certification is based on the 'Single Unix Specification' which is an extension of IEEE 1003 (POSIX).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

4. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

5. Solaris

Keyword search input from a file

Hi, I have a file which got only one column and got some keywords. I have another file where the keywords used in the first file are repeated in the second file. Now I would like to know how many times each keyword from the first file is repeated in the second file. Request your help on... (1 Reply)
Discussion started by: pointers
1 Replies

6. Shell Programming and Scripting

Merge of two input file by search

Hi i am running a issue with the way i handel open file in perl i have the following input file <File1> D33963|BNS Default Swap|-261564.923909249| D24484|BNS Default Swap|-53356.6868058492| D24485|BNS Default Swap|-21180.9904679111| D33965|BNS Default Swap|154181.478745804|... (6 Replies)
Discussion started by: kykyboss
6 Replies

7. Shell Programming and Scripting

Using an input (?) file to search another

I have a file (DCN.txt) that has about 35000 lines. It looks like: 10004470028 10005470984 10006470301 10007474812 .... I have several other files (a11.txt, a12.txt, a12_1.txt, a13.txt, etc. about 70, each 100 mb large) that have history records like so: LINE 10005470984 01/06/2010... (13 Replies)
Discussion started by: oriqin
13 Replies

8. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

9. Shell Programming and Scripting

Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this: valuearray1 contains values for param1 valuearray2 contains values for param2 for (all values in valuearray1) go into specific position in... (2 Replies)
Discussion started by: daphantomica
2 Replies

10. UNIX for Dummies Questions & Answers

multiple input search and replace script

hi, i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files. cho "" echo... (3 Replies)
Discussion started by: tungaw2004
3 Replies

Featured Tech Videos