bash/sed replace string of repeating string within a string

I have a file where each line represents data that may be encompassed with a string like "o o ". I need to replace the string between the encompassing characters with another string. The "internal" string is a pattern of two characters that may include an asterisk.
Here are some samples:
original string (OrigStrg):
" o o . . . . . . . . . . . . . . . . . . . . . . . . . . o o "
I need to replace the internal string composed of the ". " pairs with another string that might look like:
". . . . . . . . . . . . . . . * . . . . "
The second string will be read in from a file and stored in a variable.

$ echo "$OrigStrg"    
   o   o . . . . . . . . . . . . . . . . . . . . . . . . . . o   o  
$ echo "$NewLine"
            . . . . . . . . . . . . . . . * . . . .

I am relatively new to regular expressions and so my ignorance will likely be evident.
Here are some of my attempts:

$ echo $OrigStrg|sed  's/. +//g'
o o o o

I know this will get rid of the inner string, but I also recognize this is handling each ". " separately. This also gets rid of the double-spaces at the beginning and end of each line (which if possible, I’d like to retain).

These all seem to produce the same output:

$ echo "$OrigStrg" |sed -r "s/[. ]*/$NewLine/g"
$ echo "$OrigStrg" |sed -r "s/[. ]+/$NewLine/g"
$ echo "$OrigStrg" |sed -r "s/[". "]+/$NewLine/g" 
            . . . . . . . . . . . . . . . * . . . .o            . . . . . . . . . . . . . . . * . . . .o            . . . . . . . . . . . . . . . * . . . .o            . . . . . . . . . . . . . . . * . . . .o            . . . . . . . . . . . . . . . * . . . .

These seem to get rid of the original "inner string" but then seems to put the replacement every where there is a double-space in the remaining string.

echo "$OrigStrg" |sed -r "s/[. ]*/$NewLine/"
            . . . . . . . . . . . . . . . * . . . .o   o . . . . . . . . . . . . . . . . . . . . . . . . . . o   o"

This attempt seems to insert the replacement string at the first double-space but then doesn’t replace the original string (because the g at the end of the sed statement).
That seems to make sense when considering the "." as matching any single character. Which seems to be what is happening in all of these examples. I think I am close, but it is obvious I am not designating the two-character string sequence correctly.

What I am truly hoping for is this:
" o o . . . . . . . . . . . . . . . * . . . . o o "

Any help is appreciated.

Asked By: SprRdHwk

||

[. ] introduces a bracket expression that matches a dot or a space. [. ]+ matches even strings like ...., because you can’t use a bracket expression to tell sed "one character must follow the other one". You can use parentheses for that, though:

#!/bin/bash
OrigStr='o   o . . . . . . . . . . . . . . . . . . . . . . . . . . o   o'
NewStr='. . . . . . . . . . . . . . . * . . . .'

echo "$OrigStr" | sed -E "s/o( .)+ o/o $NewStr o/"
Answered By: choroba
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.