LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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



Reply
 
Search this Thread
Old 08-20-2009, 06:21 AM   #1
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Rep: Reputation: 0
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....

Last edited by Priyabio; 08-20-2009 at 06:26 AM.
 
Old 08-20-2009, 08:16 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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.

Last edited by MensaWater; 08-20-2009 at 08:20 AM.
 
Old 08-20-2009, 08:26 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
 
Old 08-20-2009, 08:40 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.
 
Old 08-20-2009, 08:42 AM   #5
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing white spaces in the file tucs_123 Linux - Newbie 9 01-03-2009 11:28 AM
How do I create words.db from words.txt using gdbm? kline General 8 12-14-2008 09:48 PM
problems in removing white spaces from string of text monil Programming 7 03-08-2005 12:28 PM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 08:48 PM
there aren't any words... salparadise General 8 01-09-2004 05:05 AM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration