Solved

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

Posted on 2014-03-19
4
838 Views
Last Modified: 2014-03-21
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.
0
Comment
Question by:Swadhin Ray
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39940800
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.
0
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 39942024
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'
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39942063
First, your command isn't quite the same as the one suggested.

You're missing the digit "1" on the right hand side between the backslash and "jdbc".
It is important because "\1" refers to the string between parentheses on the left hand side.
The end of the replacement string doesn't seem correct - shouldn't it be "</PROPERTY>" instead of "</PROPERT" ?

Second, I couldn't know that the replacement part would contain "@". I used that character as the delimiter, so the character inside the replacement string must be escaped using a backslash.

Here is a corrected version:
sed  -i.bak 's@\(<PROPERTY NAME="DBURL">\).*$@\1jdbc:oracle:thin:\@slobadb:1521:db11g</PROPERTY>@' ran.xml

Open in new window

For testing you can omit "-i.bak" so that the result will be displayed on your terminal instead of replacing the original file.
0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 39944836
thanks a lot
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question