Solved

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

Posted on 2014-03-19
4
846 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 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