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:
We added
-E
to get extended regex syntax.[:digit:]
and[:alpha:]
are used in place of0-9
andA-Z
in order to be unicode safe.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.