0

I'm attempting to parse a string that looks like 1.2.3 into something that looks like 1 : 2 : 3 using sed on Ubuntu 18.04.3 LTS.

I'm currently using the command:

$> echo 1.2.3 | sed -r 's/(\d+)\.(\d+)\.(\d+)/\1 : \2 : \3/'

Unforunately it outputs 1.2.3 instead of 1 : 2 : 3.

When I run the same command under Alpine 3.10 it works as expected.

anthonator
  • 4,915
  • 7
  • 36
  • 50
  • sed doesn't understand `+` to mean "one or more". It also doesn't understand `\d` to mean digits. Use `[0-9][0-9]*` – jhnc Nov 18 '19 at 19:05
  • Does this answer your question? [Why doesn't \`\d\` work in regular expressions in sed?](https://stackoverflow.com/questions/14671293/why-doesnt-d-work-in-regular-expressions-in-sed) – oguz ismail Nov 18 '19 at 19:09

1 Answers1

0

You may use:

echo '1.2.3' | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1 : \2 : \3/'

1 : 2 : 3

\d is a perl property for [[:digit:]] and is not available in most of the sed

You can also use:

sed -E 's/([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)/\1 : \2 : \3/' <<< "1.2.3"
anubhava
  • 761,203
  • 64
  • 569
  • 643