LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replace node(s) from xml file in shell (http://www.linuxquestions.org/questions/linux-newbie-8/replace-node-s-from-xml-file-in-shell-730448/)

BassKozz 06-03-2009 12:19 PM

Replace node(s) from xml file in shell
 
I need to replace the following:
Code:

                <connection>
                    <host><![CDATA[mysql.old_domain.com]]></host>
                    <username><![CDATA[old_username]]></username>
                    <password><![CDATA[old_password]]></password>
                    <dbname><![CDATA[old_dbname]]></dbname>
                    <active>1</active>
                </connection>

in a file located app/etc/local.xml
with
Code:

                <connection>
                    <host><![CDATA[mysql.new_domain.com]]></host>
                    <username><![CDATA[new_username]]></username>
                    <password><![CDATA[new_password]]></password>
                    <dbname><![CDATA[new_dbname]]></dbname>
                    <active>1</active>
                </connection>

but the host, username,password, and dbname variables are not always the same, so I can't simply replace "old_username" with "new_username", I need to replace the whole line:
<username><![CDATA[old_username]]></username>
with the new data:
<username><![CDATA[new_username]]></username>

What's the simplest way to do this from shell?
I need to put this into a script. Is there an easy function for dealing with xml files from bash?
TiA,
-BassKozz

fpmurphy 06-03-2009 12:56 PM

Is there an easy function for dealing with xml files from bash? No

chrism01 06-03-2009 08:18 PM

If its a one off (just a few changes), do by hand eg vim.
If you have to do a lot of changes now or over time, use Perl, its got lots of XML modules.
http://perldoc.perl.org/
http://search.cpan.org/~grantm/XML-S.../XML/Simple.pm
http://search.cpan.org/~mirod/XML-Twig-3.32/Twig.pm

i92guboj 06-03-2009 08:58 PM

I also suggest perl. I never found any formal set of routines to handle xml properly in bash. Bash is not specially powerful when it comes to parse strings. And when the thing becomes a bit more complex you have to resort to awk or sed.

ghostdog74 06-03-2009 08:59 PM

same as Perl, Python has XML parsers as well...anyway if you want to do it by hand, here's one way out of many, in Python
Code:

#!/usr/bin/env python
newdomain = "newdomain"
newuser = "newuser"
newpassword = "newpassowrd"
newdbname = "newdbname"
f=0
for line in open("/app/etc/local.xml"):
    line=line.strip()
    if "</connection>" in line: f=0;print line
    if "<connection>" in line:f=1;print line;continue
    if f:
        if "<host>" in line:
            print "\t<host><![CDATA[" + newdomain + "]]></host>"
        elif "<username>" in line:
            print "\t<username><![CDATA[" + newuser +"]]></username>"
        elif "<password>" in line:
            print "\t<password><![CDATA[" + newpassword +"]]></password>"
        elif "<dbname>" in line:
            print "\t<dbname><![CDATA[" + newpassword +"]]></dbname>"
        else: print "\t",line
    else:
        print line

output
Code:

# ./test.py
other lines
<connection>
        <host><![CDATA[newdomain]]></host>
        <username><![CDATA[newuser]]></username>
        <password><![CDATA[newpassowrd]]></password>
        <dbname><![CDATA[newpassowrd]]></dbname>
        <active>1</active>
</connection>
</connection>
last line



All times are GMT -5. The time now is 02:11 PM.