sed -e "s/[[:space:]]\+/ /g"
Here's an explanation:
[ # start of character class
[:space:] # The POSIX character class for whitespace characters. It's
# functionally identical to [ \t\r\n\v\f] which matches a space,
# tab, carriage return, newline, vertical tab, or form feed. See
] # end of character class
\+ # one or more of the previous item (anything matched in the brackets).
For your replacement, you only want to insert a space.
[:space:] won't work there since that's an abbreviation for a character class and the regex engine wouldn't know what character to put there.
+ must be escaped in the regex because with sed's regex engine
+ is a normal character whereas
\+ is a metacharacter for 'one or more'. On page 86 of Mastering Regular Expressions, Jeffrey Friedl mentions in a footnote that ed and grep used escaped parentheses because "Ken Thompson felt regular expressions would be used to work primarily with C
code, where needing to match raw parentheses would be more common than backreferencing." I assume that he felt the same way about the plus sign, hence the need to escape it to use it as a metacharacter. It's easy to get tripped up by this.
In sed you'll need to escape
). or use -r to use extended regex (then it looks like
sed -r -e "s/[[:space:]]\+/ /g" or
sed -re "s/[[:space:]]\+/ /g"