LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Updating XML files using a shell script. (https://www.linuxquestions.org/questions/programming-9/updating-xml-files-using-a-shell-script-872424/)

Cpare 04-01-2011 12:17 PM

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.

colucix 04-01-2011 03:38 PM

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.

Cpare 04-01-2011 03:45 PM

Quote:

Originally Posted by colucix (Post 4311055)
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.

theNbomr 04-01-2011 05:16 PM

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.


All times are GMT -5. The time now is 04:11 AM.