Link to home
Start Free TrialLog in
Avatar of Swadhin Ray
Swadhin RayFlag for United States of America

asked on

search partial string from a file and replace with another string using batch script and shell script

Hello Experts,

I need a batch file and a shell script for searching one string and replacing with other in an existing XML file.

Here is my XML file called "ran.xml":

<?xml version="1.0"?>
<!DOCTYPE MODULE SYSTEM "file://localhost/SYSTEMi_HOME_UNIX/Systemi/Doc/Module.dtd" [
        <!ENTITY license SYSTEM "file://localhost/SYSTEMi_HOME_UNIX/Systemi/keys/CENTNAME_license.xml">
        <!ENTITY hostname "CENTHOSTNAME">
        ]>
 
<MODULE NAME="ABC in Single Node" INDEX="&index;">
    <SYSTEM>
        <PROPERTIES>
            <!-- Begin Client App/Web Server related Properties -->
                
            <PROPERTY NAME="BC_TIMEOUT">90</PROPERTY>
           
            <!--End DB related items -->
            <INSTANCE>
                <PROPERTY NAME="DBURL">CDBURL</PROPERTY>
                <PROPERTY NAME="DBUSER">CDBUSER</PROPERTY>
            </INSTANCE>
 
            <!--Begin Log related items -->
            <PROPERTY NAME="LOGFILE">&systemi_root;/Systemi/log/client&node_id;_{0,date,MMM-dd-yy-HH-mm-ss}</PROPERTY>
            <PROPERTY NAME="LOG_ENCODING">UTF-8</PROPERTY>
            <!--End Log related items -->
                                   
</PROPERTIES>
    </SYSTEM>
</MODULE>

Open in new window


from the above script i have to search for :
<PROPERTY NAME="DBURL">..

then replace it with :

<PROPERTY NAME="DBURL">REP/STORE/CALL/DBURL</PROPERTY>


I have to search the initial only and then replace it with final string.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Do you mean a Unix/Linux shell script?

If so, here is a one-liner:
sed -i.bak 's@\(<PROPERTY NAME="DBURL">\).*$@\1REP/STORE/CALL/DBURL</PROPERTY>@' ran.xml

Open in new window

The above will create a backup file "ran.xml.bak" to then change "ran.xml" in place.
Avatar of Swadhin Ray

ASKER

I am getting the below error:


[root@msi-vmappleap1 sloba]#  sed -i.bak 's@\(<PROPERTY NAME="DBURL">\).*$@\jdbc:oracle:thin:@slobadb:1521:db11g</PROPERT@' ran.xml

sed: -e expression #1, char 54: unknown option to `s'
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks a lot