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.... |
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. |
Quote:
"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 examples using your test set: Code:
> tr -s ' ' <testfile |
As this is not a Success Story I have moved it to Linux-General.
|
All times are GMT -5. The time now is 01:17 PM. |