LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 05: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 05:26 AM.
 
Old 08-20-2009, 07:16 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 07:20 AM.
 
Old 08-20-2009, 07:26 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 07:40 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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, 07:42 AM   #5
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

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


Reply



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 10:28 AM
How do I create words.db from words.txt using gdbm? kline General 8 12-14-2008 08:48 PM
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
there aren't any words... salparadise General 8 01-09-2004 04:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:06 PM.

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
Open Source Consulting | Domain Registration