I am trying to sort my XML file that looks like this by alphabetical order. This is a part of a larger bash script so it needs to work within that script:
<Module>
<Settings>
<Dimensions>
<Volume>13000</Volume>
<Width>5000</Width>
<Length>2000</Length>
</Dimensions>
<Stats>
<Mean>1.0</Mean>
<Max>3000</Max>
<Median>250</Median>
</Stats>
</Settings>
<Debug>
<Errors>
<Strike>0</Strike>
<Wag>1</Wag>
<MagicMan>0</MagicMan>
</Errors>
</Debug>
</Module>
I want the end result to look like this, I only want the innermost tags to be sorted:
<Module>
<Settings>
<Dimensions>
<Length>2000</Length>
<Volume>13000</Volume>
<Width>5000</Width>
</Dimensions>
<Stats>
<Max>3000</Max>
<Mean>1.0</Mean>
<Median>250</Median>
</Stats>
</Settings>
<Debug>
<Errors>
<MagicMan>0</MagicMan>
<Strike>0</Strike>
<Wag>1</Wag>
</Errors>
</Debug>
</Module>
I am trying to use sort like this where -t sorts by the > delimeter and then the 4 sorts by the 4th column which would be in the inner but it is not working.
sort -t'>' -k4 file > final.xml
I get funky output that sorts the other columns in with the sorted inner tags.
Any help would be appreciated
awk --version
output)? – Ed Morton Jul 20 '21 at 22:51awk
tag from this question (I added it back) when an awk solution is very likely to be exactly what the OP needs. I disagree with the "you must have an XML parser to use XML" mantra - there's nothing wrong with using a restricted set of XML on a box that doesn't have an XML parser, it's just engineering judgement, and If the OP decided to use "Timmy's Interchange Format", we'd help them. – Ed Morton Jul 21 '21 at 13:11