LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-03-2009, 12:19 PM   #1
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Rep: Reputation: 15
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

Last edited by BassKozz; 06-03-2009 at 12:26 PM.
 
Old 06-03-2009, 12:56 PM   #2
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 286

Rep: Reputation: 61
Is there an easy function for dealing with xml files from bash? No

Last edited by fpmurphy; 06-03-2009 at 12:57 PM.
 
Old 06-03-2009, 08:18 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 06-03-2009, 08:58 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.
 
Old 06-03-2009, 08:59 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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

Last edited by ghostdog74; 06-03-2009 at 09:00 PM.
 
  


Reply

Tags
bash, replace, shell, xml


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripe replace lines in file. nenad_m Programming 5 11-27-2008 10:59 AM
perl replace script for xml ruffles Programming 8 06-17-2008 05:30 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration