$ echo "Anirudh Tomer" | sed 's/ +/ /g'
Anirudh Tomer
I was expecting it to remove those 3 spaces between Anirudh and Tomer and give me result as "Anirudh Tomer"
I am a beginner. Thanks in advance for the help.
You need to enable sed's extended regexp support with the -r flag.
echo "Anirudh Tomer" | sed -r 's/ +/ /g'
In extended regular expressions, the ?, + and | metacharacters must not be escaped (see wikipedia). The * metacharacter works because it belongs to the basic regular expressions.
anirudh@anirudh-Aspire-5920:~/Desktop/testing/GET_REQ$ echo "Anirudh Tomer" | sed 's/[ ]*/ /g' A n i r u d h T o m e r works. I mean it should also not work unless I give the -r option@Anirudh Tomer so the user is automatically notified of your comment.Similar to VIM regex, you need to escape the + quantifier with a backslash:
sed 's/ \+/ /g'
sed -r 's/ +/ /g' work