Replacing only part of matched pattern in sed

bashregexsed

For tens of thousands of lines with the same structure, i want to go (e.g.) from here:

abcd 12345 qwerty asdfg

…to here:

abcd 12345,qwerty asdfg

…with

sed 's/[0-9]\ [A-Z]/,/g'

I can match the rigth space plus both its surrounding characters (5 q), but in the replacement i get (obviously):

abcd 1234,werty asdfg

Also, i'd prefer to perform this in the linux shell

How could i match and replace for each and every line only that space which is preceded by a digit and followed by an alphabet letter? would you suggest me another tool (or even approach) instead to get this done?

Best Answer

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.

Related Question