sed: replace multiple periods with another character

You need to escape the period; period matches "any character". Further, since sed doesn't always have a + (1 or more) operator, you need to explicitly specify one followed by 0 or more, as in:

sauer@trogdor:~$ echo 'h...ello world' | sed 's/^/|/;s/\.\.*/|/;s/$/|/'
|h|ello world|

And, after rereading your question, you want a pipe at the beginning and end. So, replace ^ and $ with a pipe as well. You can do that with three -e expressions, but I like just putting all three in the command line separated by semicolons. It helps make the pattern look more like line noise. :)

If you want to match, say, 2 or more periods, use \.\.\.*. Etc. It's a shame that sed doesn't consistently support range expressions.


This should do it for you:

sed -r -e 's/\.{2,}/|/' -e 's/(.*)/|\1|/'

The {2,} just says to match 2 or more, so that if you run into periods in other places, you wont have an issue. The '-e' proceeds multiple regexes, and the '-r' means use extended regexes.

Hope it works, it does on gnu sed 4.1.2.

Tags:

Sed