Here's a solution using xml_grep (because xpath was not part of our distribution, and I did not want to add it to all production machines) ...
If you are looking for a specific parameter in an XML file, and if all the elements at a given level of the tree are unique and there are no attributes, you can use this convenient function:
# File to be parsed xmlFile="xxxxxxx"
This will work with this structure:
<config> <logs> <path>/path/to/logs</path> <logs> </config>
It will also work with this (but it will not contain newlines):
<config> <logs> <path> /path/to/logs </path> <logs> </config>
If you have duplicate <config> or <logs> or <path>, then it will only return the last one. You can probably change the function to return an array if it finds multiple matches.
FYI: This code works on RedHat 6.3 with GNU BASH 4.1.2, but I don't think I'm doing anything special, so it should work everywhere.
NOTE. For someone new to scripting, make sure you use the correct types of quotation marks, all three are used in this code (regular single quote = alphabetic, reverse single quote = execution and double quote = group).
Ed K Nov 27 '12 at 2:57 2012-11-27 14:57
source share