Remove Spaces
Spaces from text can be removed in number of ways. I will discuss about 2 ways to remove spaces.
Remove Spaces Using Python
- Replace all white spaces by single space. Here is how you can do it...
import re
text = "this is a test to remove the multiple white spaces"
text = re.sub(r'[ ]+',' ',text)
- Remove leading space in a line
import re
text = " this is a test to remove the multiple white spaces"
text = re.sub(r'^[ ]+',' ',text)
Remove white Spaces Using Sed in Bash and Zsh
- Replace all white spaces by single space
echo"this is test message
Another sentence with lot of spaces" | sed 's/[ ]\+/ /g'
- Remove leading space
echo" Another sentence with lot of spaces" | sed 's/^[ ]\+//g'