Search code examples
rrdtool

Substitute Scientific Notation from bytes to Megabytes


I Have a .xml file that has lines which look like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE rrd SYSTEM "http://oss.oetiker.ch/rrdtool/rrdtool.dtd">
<!-- Round Robin Database Dump -->
<rrd>
        <version>0003</version>
        <step>60</step> <!-- Seconds -->
        <lastupdate>1674125860</lastupdate> <!-- 2023-01-19 10:57:40 UTC -->

    <ds>
            <name> 1 </name>
            <type> GAUGE </type>
            <minimal_heartbeat>8460</minimal_heartbeat>
            <min>NaN</min>
            <max>NaN</max>

            <!-- PDP Status -->
            <last_ds>954298368</last_ds>
            <value>3.8171934720e+10</value>
            <unknown_sec> 0 </unknown_sec>
    </ds>

    <!-- Round Robin Archives -->
    <rra>
            <cf>AVERAGE</cf>
            <pdp_per_row>1</pdp_per_row> <!-- 60 seconds -->

            <params>
            <xff>5.0000000000e-01</xff>
            </params>
            <cdp_prep>
                    <ds>
                    <primary_value>8.5981579947e+08</primary_value>
                    <secondary_value>0.0000000000e+00</secondary_value>
                    <value>NaN</value>
                    <unknown_datapoints>0</unknown_datapoints>
                    </ds>
            </cdp_prep>
            <database>
                    <!-- 2023-01-17 10:58:00 UTC / 1673953080 --> <row><v>NaN</v></row>
                    <!-- 2023-01-17 10:59:00 UTC / 1673953140 --> <row><v>NaN</v></row>
                    <!-- 2023-01-17 11:00:00 UTC / 1673953200 --> <row><v>NaN</v></row>
                    <!-- 2023-01-17 11:01:00 UTC / 1673953260 --> <row><v>NaN</v></row>
                    <!-- 2023-01-17 11:02:00 UTC / 1673953320 --> <row><v>NaN</v></row>
                    <!-- 2023-01-17 11:03:00 UTC / 1673953380 --> <row><v>NaN</v></row>
                    <!-- 2023-01-18 12:00:00 UTC / 1674043200 --> <row><v>NaN</v></row>
                    <!-- 2023-01-18 18:00:00 UTC / 1674064800 --> <row><v>7.9644330667e+08</v></row>
                    <!-- 2023-01-19 00:00:00 UTC / 1674086400 --> <row><v>7.9696554667e+08</v></row>
                    <!-- 2023-01-19 06:00:00 UTC / 1674108000 --> <row><v>5.8408509440e+08</v></row>
            </database>
    </rra>

Trying to convert the scientific notation (which is a value in bytes) and convert it to a value in megabytes and back to scientific notation in Linux bash shell or script.

So far I have this lines, but i am stuck and don't know how to put them back into the file with the calculation to divide 2x by 1024:

cat Memory_mem_used.xml | grep -Eo  '[0-9]+\.[0-9]+e\+[0-9]+' | perl -ne 'printf "%d\n", $_;'

The output should look like this:

output=796443306 | output2=$(($output / 1024 / 1024)) | perl -e 'printf "%.11e\n", '$output2''
7.59000000000e+02

Solution

  • Try:

    #!/bin/bash
    IFS=''
    while read line ; do
      left=${line%%<v>*}
      rest=${line#*<v>}
      value=${rest%%</v>*}
      right=${rest#*</v>}
      if [ "$value" != "$line" ] && [ "$value" != "NaN" ] ; then # match
        num_value=$(LC_ALL=C printf '%.0f' "$value")
        new_value=$(LC_ALL=C printf '%.11e' $((num_value / 1048576)) )
        line="$left<v>$new_value</v>$right"
      fi
      echo "$line"
    done < input.xml