sed 去除特定字符周围的空格 [英] sed Removing whitespace around certain character
本文介绍了sed 去除特定字符周围的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
仅在某些字符周围删除空格的最佳方法是什么.假设破折号 - Some- String- 12345- Here
将变成 Some-String-12345-Here
.类似于 sed 's/\ -/-/g;s/-\/-/g'
但我相信一定有更好的方法.
谢谢!
解决方案
如果你的意思是所有的空格,而不仅仅是空格,那么你可以试试 \s
:
echo 'Some- String- 12345- Here' |sed 's/\s*-\s*/-/g'
输出:
<前>Some-String-12345-Here或者使用[:space:]
字符类:
echo 'Some- String- 12345- Here' |sed 's/[[:space:]]*-[[:space:]]*/-/g'
不同版本的 sed 可能支持或不支持这些,但 GNU sed 支持.
what would be the best way to remove whitespace only around certain character. Let's say a dash - Some- String- 12345- Here
would become Some-String-12345-Here
. Something like sed 's/\ -/-/g;s/-\ /-/g'
but I am sure there must be a better way.
Thanks!
解决方案
If you mean all whitespace, not just spaces, then you could try \s
:
echo 'Some- String- 12345- Here' | sed 's/\s*-\s*/-/g'
Output:
Some-String-12345-Here
Or use the [:space:]
character class:
echo 'Some- String- 12345- Here' | sed 's/[[:space:]]*-[[:space:]]*/-/g'
Different versions of sed may or not support these, but GNU sed does.
这篇关于sed 去除特定字符周围的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文