Take the 2-minute tour ×

When testing against this string:

“… so that’s that… ”

The following should, but does not, match the opening quotation mark and following ellipsis and space:

sed "s/\([“‘\"']…\) /\1/g"

However, this correctly matches the second ellipsis and following space and closing quotation mark:

sed "s/… \([”’\"'.!?]\)/…\1/g"

If I split the first apart it works fine:

sed -e "s/\(“…\) /\1/g" \
-e "s/\(‘…\) /\1/g" \
-e "s/\(\"…\) /\1/g" \
-e "s/\('…\) /\1/g"

So why doesn't it work when it's grouped together? Especially when it works fine with the closing quotation marks.

share|improve this question
I could be wrong about this, but in the second sed statement (sed "s/… \([”’\"'.!?]\)/…\1/g"), the 'dot' (in the square brackets; before the ! and ?) should match any character since it hasn't been escaped. So you're looking for the ellipsis followed by a space, followed by any one character (or any of the other listed characters). –  Creepygeek Jan 2 '10 at 21:42
That doesn't seem to be the case, as far as I can tell—the period does not seem to be acting as a wildcard when it's within the square brackets. –  Jonathan Patt Jan 2 '10 at 22:33
add comment

1 Answer

up vote 1 down vote accepted

What version of sed are you using? I believe that GNU sed should support Unicode characters, and your example works for me on Linux (Ubuntu, with UTF-8 environment).

If you are using a version of sed that is not Unicode-aware, your character group would break because it only matches one byte. If your command line is using a UTF-8 encoding, when you say a non-Unicode-aware sed would actually see three bytes, \xE2, \x80 and \x9C. This would cock up your character group which would only match one of those bytes at a time. Various other constructs would fail too, eg. a”? is the letter ‘a’ then two bytes followed by an optional third byte, so a on its own wouldn't match the expression though it looks like it should.

(You might want to consider also replacing the ellipsis character with three periods. Ellipsis is a compatibility character in Unicode; it's generally considered more modern to write out the periods and let the font take care of the typesetting.)

share|improve this answer
I'd been using whichever version of sed comes with OS X 10.6—wasn't able to determine what version that is—but I've just installed gsed and it works perfectly there. Thanks! Regarding the ellipsis, this is for a personal project where I prefer the use of ellipsis characters over three periods, and if need be, it's easy enough to convert them later. –  Jonathan Patt Jan 2 '10 at 22:20
I think you've got that backwards: ... is the compatibility version of a proper ellipsis . It is not more modern to use ... instead of , and using the ellipsis glyph is letting the font do the correct thing—that's why the ellipsis glyph is there. –  Andrew Marshall Feb 6 '12 at 0:45
add comment

Your Answer


