LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

Rep: Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985Reputation: 985
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
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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: Arch/XFCE
Posts: 17,802

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

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


Reply

Tags
trim


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
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


All times are GMT -5. The time now is 12:01 AM.

Main Menu
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