Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 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