0

I am trying to get only the value of percentage part from this data which is stored in a variable also I want to get only the first percentage value that is present in the output.

<map id="09EhL_map" name="09EhL_map">
<area shape="poly" coords="374,274,374,274,376,274,376,274,376,274,376,274" title="5-April-2019: 0.0% of runs for TT1 were successful" alt="" nohref="nohref"/>
<area shape="poly" coords="368,274,368,274,371,274,374,274,374,274,374,274" title="4-April-2019: 20.0% of runs for TT2 were successful" alt="" nohref="nohref"/>

what I have done

var1="<map id="09EhL_map" name="09EhL_map">
<area shape="poly" coords="374,274,374,274,376,274,376,274,376,274,376,274" title="5-April-2019: 0.0% of runs for HIP-HCTP-HIP3 were successful" alt="" nohref="nohref"/>
<area shape="poly" coords="368,274,368,274,371,274,374,274,374,274,374,274" title="4-April-2019: 0.0% of runs for HIP-HCTP-HIP3 were successful" alt="" nohref="nohref"/>"

var2=$(echo var1 | grep "%")

This doesn't return anything.

migrated from serverfault.com Apr 5 at 10:12

This question came from our site for system and network administrators.

0

You can use:

var2=$(echo $var1 | grep -o  [0-9][0-9]*\.[0-9][0-9]*%)

here grep -o [0-9][0-9]*\.[0-9][0-9]*% will match floating point numbers having % sign in the end.

In your case it will print

0.0%
20.0%

If you want to get only the first match, then use:

var2=$(echo $var1 | grep -o  [0-9][0-9]*\.[0-9][0-9]*% | head -n1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy