How to strip multiple spaces to one using sed?
/[ ]*/
matches zero or more spaces, so the empty string between characters matches.
If you're trying to match "one or more spaces", use one of these:
... | sed 's/ */ /g'
... | sed 's/ \{1,\}/ /g'
... | tr -s ' '
The use of grep
is redundant, sed
can do the same. The problem is in the use of *
that match also 0 spaces, you have to use \+
instead:
iostat | sed -n '/hdisk1/s/ \+/ /gp'
If your sed
do not supports \+
metachar, then do
iostat | sed -n '/hdisk1/s/ */ /gp'
Change your *
operator to a +
. You are matching zero or more of the previous character, which matches every character because everything that isn't a space is ... um ... zero instances of space. You need to match ONE or more. Actually it would be better to match two or more
The bracketed character class is also un-necessary for matching one character. You can just use:
s/ \+/ /g
...unless you want to match tabs or other kinds of spaces too, then the character class is a good idea.