I have this string:
1024.00 MB transferred (912.48 MB/sec)
and I need to get only the number 912.48 and transform it in 912,48 with a bash script.
I tried to do sed 's/[^0-9.]*//g' but in this way i get 1024.00 912.18.
How can I do it?
So far, every answer here is using external tools (sed, awk, grep, tr, etc) rather than sticking to native bash functionality. Since spinning up external processes has a significant constant-time performance impact, it's generally undesirable when only processing a single line of content (for long streams of content, an external tool will often be more efficient).
This one uses built-ins only:
# one-time setup: set the regex
re='[(]([0-9.]+) MB/sec[)]'
string='1024.00 MB transferred (912.48 MB/sec)'
if [[ $string =~ $re ]]; then # run enclosed code only if regex matches
val=${BASH_REMATCH[1]} # refer to first (and only) match group
val_with_comma=${val//./,} # replace "." with "," in that group
echo "${val_with_comma}" # ...and emit our output
fi
...yielding:
912,48
this should work
$ sed -r 's/.*\(([0-9.]+).*/\1/;s/\./,/'
echo "1024.00 MB transferred (912.48 MB/sec)" | cut -f2 -d'(' | cut -f1 -d' ' | sed 's/\./,/'
sed, go all the way and change to tr '.' ',' as well.