How to display only the content between opening and closing tags in XML file?

How to search the XML file using grep or similar for a particular tag but show only the content between opening and closing tags? Here is the exact tag I’d like to locate:


But I would like to get just the 1024000000 part and not the tag.

That is a storage size in bytes and convert that to 1 gb IF POSSIBLE or any results converted to GB.

Asked By: Joey


Using xmlstarlet to parse the XML document for any instance of the max-diskusage tag, extracting its value, and then using GNU numfmt to convert the number of bytes to SI units:

xmlstarlet sel -t -v '//max-diskusage' -nl file | numfmt --to=si

With the short example in the question in file, this returns the string 1.1G. Using --to=iec (to get the traditional power-of-two sizes) in place of --to=si, it returns 977M. Use --to=si --round=down to get 1.0G.

Answered By: Kusalananda
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.