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

It is currently Tue Oct 03, 2023 3:56 pm

Message body:
Enter your message here, it may contain no more than 60000 characters. 

:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
Font colour
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.

Topic review - SED how to remove multiple white spaces from a string
Author Message
Post subject: SED how to remove multiple white spaces from a string  |  Post Posted: Thu Feb 25, 2010 7:14 am
This article will show only a few example that would be enough for most people:

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

How to replace multiple white spaces from a string:
# 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 (|):
# echo 'some     white            spaces' | sed 's/  */\|/g'

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

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