Removing white spaces between words and joining the words in a given format
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The sign sometimes seen in an office:
"We have two speeds here---if you don't like this one, you will surely not like the other."
The point is that we are all volunteers, and we help on questions when we get around to it. AND--you are posting when many of your potential helpers are still asleep.
Your example implies that you want to reduce the number of spaces, not eliminate them completely. Please clarify if that's what you meant.
Here is just one way to do this:
Code:
sed 's/ \+/ /g' filename > newfilename ##replaces each instance of one or more spaces with a single space
If your objective is to remove extra spaces, you can also use tr -s ' ' <infile >outfile.
If you want the text aligned, you could replace the spaces with tabs: tr -s ' ' '\t' <infile >outfile
Be certain to read through the info manual for coreutils. It contains many useful programs such as fmt, tr, sort, cut that can often be piped together to perform complicated operations on text files. If you have the konqueror browser, you may find reading info files entering "info:<topic>" more convenient, or at least readable than from the terminal.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.