SPUG: multiple spaces in string

Chris Wilkes cwilkes-spug at ladro.com
Wed Feb 8 11:03:55 PST 2006

On Wed, Feb 08, 2006 at 10:56:41AM -0800, luis medrano wrote:
> List,
> I have to remove multiple spaces between a string. for example:
> "car   crash       in the freeway"
> where you can see is no uniformaty on how the spaces are showing but
> my questions is, how can remove the multiple spaces?

echo "this    has    multi spaces ." | perl -pe 's/\s+/ /g'

That converts tabs into single spaces, which might not be what you
want.  It will also take double spaces after a period and convert it
into one space.

The trick is the /g modifier which says "do this again and again until
the match no longer happens"  


More information about the spug-list mailing list