i have this string:
4017de9511f7 workflow-engine-ms 0.12% 4.609MiB / 7.574GiB 0.06% 29MB / 15.8MB 4.1kB / 0B 13
I have to extract the number 4.609, how can I do it in bash? thx!!
i have this string:
4017de9511f7 workflow-engine-ms 0.12% 4.609MiB / 7.574GiB 0.06% 29MB / 15.8MB 4.1kB / 0B 13
I have to extract the number 4.609, how can I do it in bash? thx!!
A simple way using awk
and sprintf
would be:
awk '{val=sprintf ("%g",$4); print val}' file
4.609
Using the "%g"
conversion specifier, simply converts the 4.609
portion of the 4th field, ignoring the MiB characters.
If the format does not change, you can use sed -e 's# *# #g' | cut -d\ -f4 | cut -dM -f1
to extract the value.
The sed
joins subsequent spaces into one, the first cut extract the 4.609MiB field, and the second one strips of the MiB.
If by 'I have this string' you mean, you have it in some file and you need to grab first match of X.XXX Mib (or GiB, or kB), I've solved that like this:
echo "4017de9511f7 workflow-engine-ms 0.12% 4.609MiB / 7.574GiB 0.06% 29MB / 15.8MB 4.1kB / 0B 13" > test
to prepare test file, and
grep -Po "[0-9\.]+(?=(Mi|Gi|k))" test | head -1
to find matching string