0

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:

<max-diskusage>1024000000</max-diskusage>

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.

1 Answer 1

2

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.

1
  • Works like magic!
    – Joey
    Commented Jun 18, 2022 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .