I’m trying to replace values which have a trailing minus sign and replace them with a leading minus sign. I’ve got this regex which doesn’t quite work in SED though I don’t understand why.
echo '8.8-' | sed 's/(\d+(?:\.\d+)?)-/-\\1/'
SED returns 8.8-
I tried the expression in Notepad++ RegEx Help plugin which matches the 8.8- and identifies the sub-match \1 as 8.8
The
\dis a Perlism. Sed refers to the older, pre-Perl, POSIX standard. What you probably want isThe
[[:digit:]]is the POSIX way to write Perl’s\d.Of course, you can just write
[0-9], instead, as others have noted.Incidentally, you need the
-rto make your parentheses work as you like.