Home > Software engineering >  Replace double dot in string only when there are 2 dots
Replace double dot in string only when there are 2 dots

Time:02-04

I am new to SED but learning fast. I am trying to replace 2 consecutive dots in a string with another string. The string should only 2 consecutive dots. The string is a qualified table name. The string is:

INNER JOIN DbName..TableName with (nolock)  -- comment with lots of dots ................

In the string only DbName..TableName should be replaced by DbName.PUBLIC.TableName I tried using SED with the pattern:

sed s:\(.*\[a-zA-Z0-9\]\ \)\.\.(\[a-zA-Z0-9]\*):\1.PUBLIC.\2:gi

I thought this pattern would work but when I try it the same string is returned. I entered:

echo "INNER JOIN DbName..TableName with (nolock)  -- comment with lots of dots ................" | sed s:\(.*\[a-zA-Z0-9\]\ \)\.\.\(\[a-zA-Z0-9]\*\):\1.PUBLIC.\2:gi

The output from the command was the echo string. What am I doing wrong?

echo "INNER JOIN DbName..TableName with (nolock)  -- comment with lots of dots ................" | sed  s:\(.*\[a-zA-Z0-9\]\ \)\.\.\(\[a-zA-Z0-9]\*\):\1.PUBLIC.\2:gi

I was expecting:

INNER JOIN DbName.PUBLIC.TableName with (nolock)  -- comment with lots of dots ................"

CodePudding user response:

Put the sed argument inside quotes, otherwise \. becomes simply . when the shell processes the escape sequence.

sed 's/\.\./.PUBLIC./g'

CodePudding user response:

I suggest with GNU sed:

sed -E 's/([^.])\.\.([^.])/\1.PUBLIC.\2/'

See: The Stack Overflow Regular Expressions FAQ

  • Related