Use sed with back references

The problem is quoting.

Because you don't quote your sed command, the parenthesis \(...\) was interpreted by the shell before passing to sed.

So sed treated them as literal parenthesis instead of escaped parenthesis, no back-reference affected.

You need:

echo "312.2 MB" | sed 's/\([0-9]\)[[:space:]]\([GMK]\)/\1\2/g'

to make back-reference affected, and get what you want.

Or more simply:

echo "312.2 MB" | sed 's/ //'

You ain't quoting any of your sed expressions, that is the main culprit. put quotes around it like sed ' '. Or Simply you can get that by following tr expression,

tr -d '[:space:]' <<< "312.2 MB"

tr -d ' ' <<< "123.34 KB"

tr -d '[:blank:]' <<< "487.1 GB"

If you are insisting on sed, you can do that by,

sed 's/ //g' 
sed 's/[[:blank:]]//g' 
sed 's/[[:space:]]//g' 

use bash:

$ txt="32.2 MB"
$ echo ${txt// /}