LinuxQuestions.org
Help answer threads with 0 replies.
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 03-02-2009, 05:09 AM   #1
manohare
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Rep: Reputation: 0
Thumbs down What is the unix command to trim multiple spaces into single space?


Hi,
Can any one suggest the unix command which trims the multiple spaces into single space.
 
Old 03-02-2009, 05:15 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
tr -s ' '
 
Old 03-02-2009, 05:50 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Works for spaces (as was asked for), but I prefer to use something more generic, like "sed -r -i 's/\s+/ /g' <file>".
 
Old 03-02-2009, 06:17 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
Tr is a basic and posix standard command. You could use '[[:space:]]' as a whitespace wild card character in tr or sed.
I think that \s is GNU specific. Next someone will chime in to use a perl command with even more arcane syntax.
 
Old 03-02-2009, 07:25 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed 's/ */ /g'

sed 's/[[:blank:]]*/ /g'  ##replaces whitespace (eg spaces or tabs)
 
  


Reply

Tags
trim



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
combining multiple sed operations into a single command kushalkoolwal Programming 3 09-16-2008 05:58 PM
trim heading and trailing space George2 Programming 1 10-21-2007 03:07 AM
How to run multiple process in single command prompt babu198649 Linux - Newbie 7 10-12-2007 08:27 AM
using multiple -exec commands in a single find command? Bluemilk Linux - Newbie 4 03-19-2006 10:46 AM
Compiling multiple .c file using single command in makefile vipulc Linux - General 2 03-18-2006 11:49 PM

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

All times are GMT -5. The time now is 01:52 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
Open Source Consulting | Domain Registration