LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Removing white spaces between words and joining the words in a given format (http://www.linuxquestions.org/questions/linux-general-1/removing-white-spaces-between-words-and-joining-the-words-in-a-given-format-748972/)

Priyabio 08-20-2009 06:21 AM

Removing white spaces between words and joining the words in a given format
 
Hi,
Please tell me how to remove white spaces in between the words in the file using linux commands?

My actual data in the text file is as below with spaces.

input file:

Q5ST30....2.........P42336.......................................................................... ..... 29
P78417................ 3............. Q92993
Q13233 ............. 3 ............................ O14920 ........................................................................ 15


I want the output in the below given format

Q5ST30... 2... P42336... 29
P78417... 3... Q92993 ...
Q13233... 3... O14920... 15

The above files dot (.)denote the space


Please reply me asap....

MensaWater 08-20-2009 08:16 AM

awk '{print $1" "$2" "$3" "$4}' <filename>

Where you type in the number of spaces you want between each of the double quotes.

The default delimiter for awk is space so your fields will be $1, $2, $3 and $4 (on the lines that have a 4th field).

You can achieve even better formatting printf instead of print but the above is a good quick and dirty for your purposes.

pixellany 08-20-2009 08:26 AM

Quote:

Please reply me asap...
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

jschiwal 08-20-2009 08:40 AM

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

examples using your test set:
Code:

> tr -s ' ' <testfile
Q5ST30 2 P42336 29
P78417 3 Q92993
Q13233 3 O14920 15
> tr -s ' ' '\t' <testfile
Q5ST30  2      P42336  29
P78417  3      Q92993
Q13233  3      O14920  15

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.

XavierP 08-20-2009 08:42 AM

As this is not a Success Story I have moved it to Linux-General.


All times are GMT -5. The time now is 05:26 PM.