RubyGuides
Share this post!

Squashing spaces

If we want to remove extra spaces in a text like this:

This    is some interesting     text.

You could be tempted to use sed, but there is a simpler way using tr

tr -s ' '

And we end up with:

This is some interesting text.

With tr you canĀ also delete characters instead of compressing them. Let’s say we wanted to get rid of the vowels.

tr -d 'aeiou'

We get:

Ths s sm ntrstng txt.