replacing only part of matched pattern in sed

Try:

$ echo 'abcd 12345 qwerty asdfg' | sed -E 's/([[:digit:]]) ([[:alpha:]])/\1,\2/g'
abcd 12345,qwerty asdfg

Notes:

  1. We added -E to get extended regex syntax.

  2. [:digit:] and [:alpha:] are used in place of 0-9 and A-Z in order to be unicode safe.

  3. Parens are used to create groups and we can reference in the replacement text. In our case, \1 references the number and \2 references the letter.