Bri*_*ica 211 string bash replace
我需要能够做的是用bash中的字符串中) with a dot (
.
)替换space().
我认为这很简单,但我是新手,所以我无法弄清楚如何修改这个用途的类似示例.
Bri*_*per 362
使用内联shell字符串替换.例:
foo=" "
# replace first blank only
bar=${foo/ /.}
# replace all blanks
bar=${foo// /.}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://tldp.org/LDP/abs/html/string-manipulation.html.
aio*_*obe 72
你可以使用tr
,像这样:
tr " " .
Run Code Online (Sandbox Code Playgroud)
例:
# echo "hello world" | tr " " .
hello.world
Run Code Online (Sandbox Code Playgroud)
来自man tr
:
描述
从标准输入翻译,挤压和/或删除字符,写入标准输出.
Gil*_*il' 49
在bash中,您可以使用构造在字符串中进行模式替换${VARIABLE//PATTERN/REPLACEMENT}
.使用just /
而不是//
仅替换第一次出现.该模式是一个通配符模式,如文件globs.
string='foo bar qux'
one="${string/ /.}" # sets one to 'foo.bar qux'
all="${string// /.}" # sets all to 'foo.bar.qux'
Run Code Online (Sandbox Code Playgroud)
小智 8
尝试这个路径:
echo \"hello world\"|sed 's/ /+/g'|sed 's/+/\/g'|sed 's/\"//g'
Run Code Online (Sandbox Code Playgroud)
它用单引号替换双引号字符串内的空格+
,然后+
用反斜杠替换符号,然后删除/替换双引号。
我必须使用它来替换 Cygwin 中路径之一中的空格。
echo \"$(cygpath -u $JAVA_HOME)\"|sed 's/ /+/g'|sed 's/+/\\/g'|sed 's/\"//g'
Run Code Online (Sandbox Code Playgroud)