Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-03-2009, 12:19 PM   #1
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:
in a file located app/etc/local.xml
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:
with the new data:

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?

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

Rep: Reputation: 62
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
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,198

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

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
#!/usr/bin/env python
newdomain = "newdomain" 
newuser = "newuser"
newpassword = "newpassowrd"
newdbname = "newdbname"
for line in open("/app/etc/local.xml"):
    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
        print line
# ./
other lines
last line

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


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 07:39 PM.

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