Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

SED how to remove multiple white spaces from a string
https://forum.ivorde.com/sed-how-to-remove-multiple-white-spaces-from-a-string-t60.html
Page 1 of 1

Author:  debuser [ Thu Feb 25, 2010 7:14 am ]
Post subject:  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.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/