Solved

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

Posted on 2014-03-19
4
825 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks a lot
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now