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:
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.