Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
$ echo "Anirudh Tomer" | sed 's/ +/ /g' Anirudh Tomer
我期待它可以删除 Anirudh 和 Tomer 之间的这 3 个空格,并给我结果"Anirudh Tomer"
"Anirudh Tomer"
我是初学者。在此先感谢您的帮助。
您需要使用该-r标志启用 sed 的扩展正则表达式支持。
-r
echo "Anirudh Tomer" | sed -r 's/ +/ /g'
在扩展正则表达式中?,+和|元字符不得转义(请参阅wikipedia)。元*字符之所以起作用,是因为它属于基本的正则表达式。
?
+
|
*
与 VIM 正则表达式类似,您需要+使用反斜杠转义量词:
sed 's/ \+/ /g'
echo "Anirudh Tomer" | tr -s ' '