Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Sun Aug 20, 2017 4:46 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Author Message
debuser
  Post  Post subject: SED how to remove multiple white spaces from a string  |  Posted: Thu Feb 25, 2010 7:14 am

Joined: Thu Aug 06, 2009 2:48 am
Posts: 105

Offline
 

SED how to remove multiple white spaces from a string

This article will show only a few example that would be enough for most people:

How to remove multiple white spaces from a string:
Code:
# echo 'some     white            spaces' | sed 's/  *//g'
somewhitespaces


How to replace multiple white spaces from a string:
Code:
# echo 'some     white            spaces' | sed 's/  */\ /g'
some white spaces


Above sed command replaces multiple spaces with a single space. You can adjust it to replace multiple spaces with other characters, like pipe (|):
Code:
# echo 'some     white            spaces' | sed 's/  */\|/g'
some|white|spaces


Special attention needs to be paid to the start and end of the lines:
Code:
# echo '             some     white            spaces ' | sed 's/  */\|/g'
|some|white|spaces|


Above example contains multiple spaces at the beginning and one space at the end of the line. Sed replaced all with a pipe.
All of the above sed regular expressions in the replaced field (/ */) contain two spaces, followed by a ster (*). Omitting this will mess up your script.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "SED how to remove multiple white spaces from a string"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

4855

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. How to replace \w SED multiple matches on the same line without piping

mandrei99

0

1051

Tue Oct 08, 2013 6:29 am

mandrei99 View the latest post

There are no new unread posts for this topic. Remove empty lines in vi/vim

debuser

0

522

Mon Apr 26, 2010 11:06 am

debuser View the latest post

There are no new unread posts for this topic. Echo new line in linux - supress new line or echo multiple new lines

mandrei99

0

2788

Wed Jan 14, 2015 5:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Remove the character before the last character with SED - shell scripting

mandrei99

0

1475

Wed Nov 04, 2009 10:49 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
cronNews News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO