Remove multiple spaces

sed "s/^ *//;s/ *$//;s/ \{1,\}/ /g" filename.txt
The command removes all the spaces whithin a file and leaves only one space.
Sample Output
Original: AAA   AAA AAA
After sed: AAA AAA AAA

2011-03-09 10:35:02

What Others Think

We can shorten that a little. First \{1,\} is equivalent to \+ Next, by rearranging the sed clauses, we can shorten the beginning/end of line bits: sed "s/ \+/ /g;s/^ //;s/ $//" filename.txt If you want to flatten all whitespace (tabs as well as spaces), replace the first space character with \s
flatcap · 596 weeks and 6 days ago
The command lines are the developed type of situation with the huge existence of category wise junction in the segment wise partition. The set here has the multiple spaces assigned in the parts of proessaywriting with the housing availability in the cloud sets of parts versions.
puguherasa · 68 weeks and 4 days ago
This link will let you know more about which writing services are worth your attention. It's very educational and can save you time. More information here.
MichaelWilson1 · 45 weeks and 6 days ago ????? ??? ????? ???? SEO ????? ????? ????? ???? ????? ????? ??? ???? ???? ??????? Search Engine Optimization ??? ????? ??????????? ?? ????? ?????????? ????? ????? ?? ????
seofox · 28 weeks and 2 days ago
seofox · 27 weeks ago
seofox · 27 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: