Linux 如何删除shell脚本中字符串中所有出现的点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4332392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove all occurences of dot in a string in a shell script?
提问by rupali
e.g hostname = "test.test.test", then after removing result should be like "testtesttest"
例如hostname =“test.test.test”,然后删除结果应该像“testtesttest”
回答by Ignacio Vazquez-Abrams
$ foo=test.test.test
$ echo "${foo//./}"
testtesttest
回答by darioo
A general way would be to pipe it to sed:
一般的方法是将它通过管道传输到 sed:
sed -e 's/\.//g'
On command prompt:
在命令提示符下:
$ echo $hostname // you type this
test.test.test // this is the result
$ echo $hostname | sed -e 's/\.//g'
testtesttest
回答by codaddict
You can also pipe into
您也可以通过管道输入
tr -d '.'
But the best way of doing this is not to use an external command and use shell built in.
但最好的方法是不要使用外部命令并使用内置的 shell。
回答by davidcondrey
Not just . but - and _ as well
不只是 。但是 - 和 _ 也是
HOSTNAME=test.test.test
HOSTNAME=${HOSTNAME//[-._]/} # testtesttest