LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2011, 12:17 PM   #1
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 73

Rep: Reputation: 15
Updating XML files using a shell script.


Hello everyone,
I continue to work on automating the update and deployment of a vendors WAR files, and have bumped into my next challenge...

The vendor provides web.xml files have entries that look like this
Code:
    <context-param>
        <param-name>siteminder.enabled</param-name>
        <param-value>false</param-value>
    </context-param>
I need to search the file for a param-name and replace the param-value below it with the correct value. I expect sed or awk is the trick on this, but I am not sure how to have it search for one line, and have it update the line below it - any help would be appreciated.

Last edited by Cpare; 04-01-2011 at 12:20 PM. Reason: Spelling errors
 
Old 04-01-2011, 03:38 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try the n command of sed. Every time it encounters the pattern, it prints the current pattern (the matching line left untouched), empties the pattern space and read in the next line. At this point you can apply the substitution. Example:
Code:
$ sed '/siteminder.enabled/{n; s/false/true/}' file
    <context-param>
        <param-name>siteminder.enabled</param-name>
        <param-value>true</param-value>
    </context-param>
More details on the grymoire's Sed tutorial (a must-read). Hope this helps.
 
Old 04-01-2011, 03:45 PM   #3
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 73

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
You can try the n command of sed. Every time it encounters the pattern, it prints the current pattern (the matching line left untouched), empties the pattern space and read in the next line. At this point you can apply the substitution. Example:
Code:
$ sed '/siteminder.enabled/{n; s/false/true/}' file
    <context-param>
        <param-name>siteminder.enabled</param-name>
        <param-value>true</param-value>
    </context-param>
More details on the grymoire's Sed tutorial (a must-read). Hope this helps.
Thanks for the assist - The sample above isn't working for me, but it could be because I am on AIX 5.3 - I will check the sed guide and break this down to see where it's falling over, if I get it to work I will be sure to post it back here.
 
Old 04-01-2011, 05:16 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
As I and others have pointed out numerous times on these forums, sed is a poor tool to use for XML parsing and editing, since it will invariably depend on some kind of formatting that is subject to alteration by other tools and by the tool that creates the HTML (especially if that tool is a human). Instead, use a proper, robust XML parser such as one of the ones supported for Perl on CPAN.

--- rod.
 
1 members found this post helpful.
  


Reply


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
How to find the tag value in xml file through unix shell script hussain.s Linux - General 2 11-03-2010 07:52 AM
How to delete lines and insert another line in some xml files using a shell script? luananeko Linux - Newbie 1 07-20-2009 09:53 PM
Modifying Specific Child Nodes In XML using Shell Script senthilmuthiah Linux - Newbie 1 04-20-2009 04:38 AM
mp3 filename to xml shell script jason7 Linux - Server 1 03-21-2009 05:22 AM
Shell script for website updating Cron Programming 2 03-06-2005 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration